I have four threads makes processing on four files, then I want a thread to concatenate these files.
My solution is to make a fifth thread (thread1) to concatenate.
sum = new Thread() {
public void run() {
if (thread1.isAlive()) {
synchronized (lock1) {
while (thread1.isAlive()) {
try {
lock1.wait();
} catch (InterruptedException ex) {
Logger.getLogger(mainClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
if (thread2.isAlive()) {
synchronized (lock2) {
while (thread2.isAlive()) {
try {
lock2.wait();
} catch (InterruptedException ex) {
Logger.getLogger(mainClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
if (thread3.isAlive()) {
synchronized (lock3) {
while (thread3.isAlive()) {
try {
lock3.wait();
} catch (InterruptedException ex) {
Logger.getLogger(mainClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
if (thread4.isAlive()) {
synchronized (lock4) {
while (thread4.isAlive()) {
try {
lock4.wait();
} catch (InterruptedException ex) {
Logger.getLogger(mainClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} // new MakeDataSet(path + "f4", "d4.arff");
The problem occurred if the threads don't finish in the order of their indices (like thread3 finishes before thread2 or thread1 or when thread4 finished before thread3/thread2/thread1) in which case the program never ended.
Thread.join()
them in the main thread? – ivant