File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes How to check whether multiple threads execution is completed or not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to check whether multiple threads execution is completed or not?" Watch "How to check whether multiple threads execution is completed or not?" New topic
Author

How to check whether multiple threads execution is completed or not?

Shankara Sharma
Ranch Hand

Joined: Feb 06, 2012
Posts: 41
Hi,
for loop
{
// A code that starts multiple datasources back up.
// Each datasouce back up will be done by single thread.
}
getData();
I have started multiple threads inside for loop and i want to invoke getData() method after completion of mutiple threads execution that i have started inside for loop. Please help me on this.
Thanks,
Shankar.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Hi Shankara,

Maybe this thread could help. I'm not much aware of CountDownLatch, however, the thread I mentioned seems to have similar question as yours.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You can use a countdown latch to wait until the threads are done, and for slightly subtler situations, such as waiting until, say, "any 5 of the 10 threads I just started complete", it's the way to go. But if you have a particular set of threads and you just want to wait until all of them have completed, the easiest way is just to call join() on all of them.
Shankara Sharma
Ranch Hand

Joined: Feb 06, 2012
Posts: 41
Thanks Shivalkar and Jeff... That was really helpful.
But when we use join() method... threads will be executed one after another right?
What i need is to execute all threads in parallel and after their execution is completed then i want to got to getData() method. Is there any way to check this?
Actually, this getData() method will write some property information into properties file. So to access that file we need to obtain ownership, save information and release ownership of the file.

Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Shankara Sharma wrote:Thanks Shivalkar and Jeff... That was really helpful.
But when we use join() method... threads will be executed one after another right?


Only if you use it incorrectly.



Note that in the "right" case, it doesn't stop t1, t2, and t3 from running concurrently. It just stops this thread--the one calling join--from going any further until all 3 threads have completed.



Shankara Sharma
Ranch Hand

Joined: Feb 06, 2012
Posts: 41
This is correct way

and this is wrong way

Right?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Correct. Doing it the wrong way (your second way, my first way) is the same as not using threads at all and just exeucuting everything sequentially instead.
Shankara Sharma
Ranch Hand

Joined: Feb 06, 2012
Posts: 41
Thanks a lot Jeff...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're very welcome!
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 47

Word of advice:

Though thread.join() is good solution, i think using a CountDownLatch is a perfect solution. Reasons

- Code will be a bit cleaner
- Offers flexibility in terms of scaling; in future if handling threads explicitly is to be replaced. There are couple of nice APIs like java.util.concurrent.Executor




Warm Regards,
Rakesh
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18138
    
    8

Yes, absolutely. It takes a little while to get used to the classes in the java.util.concurrent package but once you do, it's much simpler to write concurrent code. And just as important, the concurrent code you write is more likely to be correct.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The one advantage that I can think of to using join() instead of a CDL is that the Runnable don't have to know anything about it. Not that that's particularly compelling in most cases, but I thought it was worth mentioning.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to check whether multiple threads execution is completed or not?
 
Similar Threads
Marcus Green Mock- doubt
strange results from wait() and notify()
Threading and collections problem.Pl help
Threads Completed Notification
why one thread runs