# Partition numbers as polynomial, each number in a thread

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[];

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));
}
}
notify();
}
}

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

try {
wait();
}
} catch (InterruptedException e) {
}
try {
wait();
}
} catch (InterruptedException e) {
}

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!
• @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. – HMD Feb 13 '17 at 18:30