Reputation: 7143
I am implementing java Runnable
interface for multithreading. I have some n
number of threads. Each of the thread has its own life. I would like to wait till life of all the threads get expire. Lets say following is the case
for(int i = 0; i< k;i++){
Thread thread1 = new Thread(new xyz())
Thread thread2 = new Thread(new abc())
Thread thread3 = new Thread(new mno())
thread1.start();
thread2.start();
thread3.start();
}
I am doing following to synchronize it. I don't know if it is correct. Please let me know how would I do it? And is there any way to check, if my threaded program is working correctly?
if(thread2.isAlive())
try {
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(thread1.isAlive())
try {
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(thread3.isAlive())
try {
thread3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Upvotes: 4
Views: 159
Reputation: 328835
You could add your Runnables to an ExecutorService and call shutdown / awaitTermination which will return once all tasks have completed. There are a few examples in the javadoc - in summary you would write something like:
ExecutorService executor = Executors.newFixedThreadPool(3);
executor.submit(runnable1);
executor.submit(runnable2);
executor.submit(runnable3);
executor.shutdown();
boolean allRunnableAreDone = executor.awaitTermination(60, TimeUnit.SECONDS);
// This line is reached once all runnables have finished their job
// or the 60 second timeout has expired
Upvotes: 8
Reputation: 116918
Although the ExecutorService
answer from @assylias is a good one, here's more information about join()
.
You do not need to test the isAlive()
before you join()
. The Thread.join()
code already does that. Here's a snippet from the code there:
while (isAlive()) {
wait(0);
}
So all you need to do is join with your threads:
try {
thread2.join();
thread1.join();
thread3.join();
} catch (InterruptedException e) {
// always a good pattern
Thread.currentThread().interrupt();
e.printStackTrace();
}
Upvotes: 4
Reputation: 4443
The join()
method does not join on a thread, but on a lock object. The waiting thread must call lockObject.join()
and the working thread must call lockObject.notify()
when it is done. The waiting thread will be notified and can continue its work. You also need synchronize
blocks around these calls.
I also recommend the Executor like assylias mentioned, it is much easier than implementing this behaviour by yourself.
Upvotes: 1
Reputation: 61540
As a simple solution you could put all of your threads in a List then call join on them after they have been started:
List<Thread> threads = new ArrayList<>();
for(int i = 0; i< k;i++)
{
//create threads
Thread thread1 = new Thread(new xyz());
Thread thread2 = new Thread(new abc());
Thread thread3 = new Thread(new mno());
//store threads
threads.add(thread1);
threads.add(thread2);
threads.add(thread3);
//start threads
thread1.start();
thread2.start();
thread3.start();
}
//join all threads
for(Thread t : threads)
t.join();
//You are here after all threads have terminated
Upvotes: 2
Reputation: 382434
Java contains mechanism to do this kind of things in java.util.concurrent.
In your case, you probably need CountDownLatch or ExecutorService
Upvotes: 1