wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes How to wait for multiple thread to finish run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to wait for multiple thread to finish run" Watch "How to wait for multiple thread to finish run" New topic
Author

How to wait for multiple thread to finish run

Suneet Kamath
Greenhorn

Joined: Sep 07, 2002
Posts: 5
I need to wait for multiple threads to finish the run so as to accumulate results
The situation is more easily described below

Output:
Spawning Threads
Making 1
Making 2
Making 3
Making 4
Making 5
Thread 1(5)
Thread 2(5)
Thread 3(5)
Thread 4(5)
All Threads Started and ran. Exiting
Thread 5(5)
Thread 1(4)
Thread 2(4)
Thread 3(4)
Thread 4(4)
Thread 5(4)
Thread 1(3)
Thread 2(3)
Thread 3(3)
Thread 4(3)
Thread 5(3)
Thread 1(2)
Thread 2(2)
Thread 3(2)
Thread 4(2)
Thread 5(2)
Thread 1(1)
Thread 2(1)
Thread 3(1)
Thread 4(1)
Thread 5(1)
Only after all the threads finish running should the foll message appear
All Threads Started and ran. Exiting
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the ranch Suneet.
Is it workable having a reference to each thread? You could call join on each of them.


SCJP2. Please Indent your code using UBB Code
Suneet Kamath
Greenhorn

Joined: Sep 07, 2002
Posts: 5
Jose. I didnt quite get what u mean. Please could you be more explicit. An example will definitely be more help.
Again I only want the calling thread (either main or the Simple Thread Spawn) to be waiting for all threads to exit run.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
At the time of creation of the threads add them to a n ArrayList. The thread that must wait for all of them could simply loop through the ArrayList calling join on each thread.
Maybe not too elegant, maybe others solutions are possible. But this seems simple.
[ September 07, 2002: Message edited by: Jose Botella ]
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Something like this:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Why would you join() a thread immediately after start()? This has the effect of waiting for that thread to complete before starting any other thread. Why use threads at all in that case? I The key is to start all the threads first, and then use join() to wait for them all to finish:

[ September 09, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Suneet Kamath
Greenhorn

Joined: Sep 07, 2002
Posts: 5
Thanks for the reply guys. Jim ur solution worked..... but duh .... you knew it would right . Great!!!
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Thanks for pointing it out Jim
Manish Veerwal
Greenhorn

Joined: Sep 09, 2009
Posts: 1
you can use the JAVA APi for that purpose. Use ExecutorService Interface like below:
if you want to wait for 4 threads to complete....

ExecutorService taskExecutor = Executors.newFixedThreadPool(4);
while(...) {
taskExecutor.execute(new MyTask());
}
taskExecutor.shutdown();
try {
taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
...
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

Manish Veerwal wrote:you can use the JAVA APi for that purpose. Use ExecutorService Interface like below:
if you want to wait for 4 threads to complete....


Considering that this topic ...

1. has been considered as solved.
2. is seven years old.

I think that it is safe to assume that the original poster of this topic, is no longer waiting for an answer.


And BTW, welcome to the JavaRanch...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to wait for multiple thread to finish run
 
Similar Threads
thread class execution explanation required
Finding a prime number
join()
Thread
Updated MulitThreads Problem