Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

using join() on multiple threads.  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm wondering, if I have multiple worker threads and want the created thread to wait for them all to finish. Would this be the right way to do it:

 
Sheriff
Posts: 24086
54
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

Although in a real program you would more likely write a loop to join all of the threads, rather than an unrolled loop.
 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carlton Hanna wrote:I'm wondering, if I have multiple worker threads and want the created thread to wait for them all to finish. Would this be the right way to do it:



You mean to say that created Thread is Main Thread, and should it wait for the worker thread to finished their jobs.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Won't this do rather than looping again to join the threads again???

 
Ranch Hand
Posts: 3173
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isuru: no. That would completely neutralize the point of using multiple threads. Every time you start a new thread, you wait until it completes, doing nothing else until it completes. In that case, you might as well do all the work in one thread - all benefits of using multiple threads have been lost.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can use either a CyclicBarrier or a CountDownLatch. Its an elegant way for the main thread to wait for its worker threads to finish. Again it depends on your code wether you need to use a CyclicBarrier or a CountDownLatch.

_SM

 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,

Thanks a lot for your explanation. Saved me from lots of trouble. Now I get it. The moment I call the join() method my calling thread will start waiting untill the spawned thread completes.
 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steven,

Thanks for your suggestion. CountDownLatch is what I need. Thanks again.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!