3
\$\begingroup\$

I'm beginner at multithreading programming and I wanna write a piece of my code using it. I'm trying to split different numbers into multiple parts and store them as a polynomial. I wanna do this using multithreading as each number being splitted in one thread, all simultaneously. For example, 5 separate threads split 5 different numbers.

Suppose I have a class named Polynomial. Assume that there's just two numbers that I wanna create polynomials out of them. They are initialized and stored already and I wanna do the partition task on both of them simultaneously.

Number is a class that stores numbers. n_Polynomial and m_Polynomial store results. I created a method named partitionNumbers().

Class Polynomial{
Number n,m,n_Polynomial[],m_Polynomial[],

private void partitionNumbers() {

        class Utility extends Thread {

            Number number; 
            Number parts[]; 
            boolean threadHasDoneTask = false;

            Utility(Number number) {
                this.number = number;
            }

            Number[] getParts() {
                return this.parts;
            }

            @Override
            public void run() {
                int partNumbers = (number.getReal().getIntegerPartLength() + number.getReal().getFractionalPartLength()) / base;
                if (partNumbers <= 1) {
                    partNumbers = 1;
                } else {
                    partNumbers++;
                }
                this.parts = new Number[partNumbers];
                String temp = this.number.toString();
                int index = temp.indexOf('.');
                if (index != -1) {
                    temp = new StringBuilder(temp).delete(index, index + 1).toString();
                }
                int length = temp.length();
                for (int i = partNumbers - 1; i >= 0; i--) {
                    try {
                        parts[i] = new Number(temp.substring(length - base, length));
                        length -= base;
                    } catch (IndexOutOfBoundsException e) {
                        parts[i] = new Number(temp.substring(0, length));
                    }
                }
                threadHasDoneTask = true;
                notify();
            }
        }

        Utility thread1 = new Utility(m);
        Utility thread2 = new Utility(n);
        thread1.start();
        thread2.start();

        try {
            while (!thread1.threadHasDoneTask) {
                wait();
            }
        } catch (InterruptedException e) {
        }
        m_Polynomial = thread1.getParts();
        thread1.interrupt();
        try {
            while (!thread2.threadHasDoneTask) {
                wait();
            }
        } catch (InterruptedException e) {
        }
        n_Polynomial = thread2.getParts();
        thread2.interrupt();
    }
}

I know using wait/notify idiom is wrong here, but I just can't find any other way to tell main thread that thread1 and thread2 are done!

What are my solution problems and is it generally reliable?

\$\endgroup\$
  • 1
    \$\begingroup\$ I wouldn't like to see a class definition inside a method.Just write the class declaration somewhere else \$\endgroup\$ – cssGEEK Feb 13 '17 at 17:47
  • \$\begingroup\$ @cssGEEK Utility is just for calculations and only partitionNumbers() uses its instance. i'm not sure it is better to create a separate java file for it. \$\endgroup\$ – HMD Feb 13 '17 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.