Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When to use join()?

 
Mike Rolf
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone give me a good example of when you would want to use the join() method of the Thread class?

I'm well aware of what the method does, I just cant think of a good example of when I'd use it.

Cheers,

Mike
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Old Days, before the java.util.concurrent package was added to the API, a common use of join() was to know when a set of parallel tasks was completed. You'd create a group of threads to run some tasks in parallel, storing references to all the threads in an array. Then you loop over the array and start all the threads. Then you loop over the array and call join() on each one. When that second loop completes, you know that all your tasks are done. What kind of tasks? Think downloading multiple files, or factoring a list of primes, or compiling a list of files, or anything else that can be parallelized, and you don't want to proceed until every task is complete.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!