Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Shankara Sharma
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is correct way

and this is wrong way

Right?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Jeff...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're very welcome!
 
Rakesh K. Cherukuri
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Paul Clapham
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic