// A code that starts multiple datasources back up.
// Each datasouce back up will be done by single thread.
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.
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.
Joined: Feb 06, 2012
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.
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.
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.