aspose file tools*
The moose likes Threads and Synchronization and the fly likes ThreadPool Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ThreadPool Question" Watch "ThreadPool Question" New topic
Author

ThreadPool Question

Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42
Hi,

I am trying to learn multi-threading, as part of that i am trying to implement thread pool. In below main method i am passing runnable object to pool.execute() method to run that in a separate thread from pool , now how do i wait till all the threads are completed to start processing the result.



Thanks,
Suri
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The way to wait for Thread T to die is to call T.join(). However, that's probably not appropriate here for two reasons.

1) You normally don't have direct access to the Threads in a pool.

2) In a thread pool, the Threads usually continue to live beyond the execution of a single task. That's kind of the point of what thread pools are for and how they work.

Your threads might do something like this, in a loop:


One easy way to do the last step--the signalling--is to use a java.util.concurrent.CountDownLatch. Set it to 1, pass it to the thread pool with the task to execute, and have the thread pool call countDown(). The main thread calls await().
Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42
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. 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 ..

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



Thanks in advance.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ThreadPool Question