• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How to wait for multiple thread to finish run  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch Suneet.
Is it workable having a reference to each thread? You could call join on each of them.
 
Suneet Kamath
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Suneet Kamath
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply guys. Jim ur solution worked..... but duh .... you knew it would right . Great!!!
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing it out Jim
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) {
...
}
 
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Everybody's invited. Even this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!