Surender Suri wrote:Thanks Jeff.
After using the CountDownLatch, it is working as expected atleast with the initial test with no null exceptions. Is this the correct way to use CountDownLatch.
Why are you calling countDown() at the start of your task?
You should call it at the end.
Also, i see their is one more way to do this by using Semaphore, with the release() and acquire() , what is the difference between them ..
You could do that I guess. I feel like a CDL is better suited to the task though. It's meant for waiting until some event happens. You can achieve the same results with a Semaphore here, but that's not really its purpose.
Secondly i don't understand how the Semaphore works at all , i mean where is the link which holds the threads to wait .. it say's it's just a counter adding the permits when release() is called
I don't really understand what you're asking here. If you're curious as to how it works, you can look at the source code. It's in src.zip, which came with your JDK download.
Finally, though, all this is pretty much irrelevant here. I didn't really look at your code before. I thought you were implementing your own thread pool. Since you're just using the existing ThreadPoolExecutor, you don't have to do the explicit CDL. You can use one of the submit or invoke methods, and then call get() on the returned Future.