permaculture playing cards*
The moose likes Threads and Synchronization and the fly likes Join in a pool thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Join in a pool thread" Watch "Join in a pool thread" New topic
Author

Join in a pool thread

Rodrigo Bossini
Ranch Hand

Joined: Jul 03, 2009
Posts: 113
Hi,

I'm using Executors.newFixedThreadPool to get a pool and manage my threads.

I'd like to be able to "join" a thread that belongs to this pool.

Is it possible?

A solution I thought about, was to send an object to the Runnable constructor, and have the thhread that invokes the thread in the pool to "wait" in this object, have the thread in the pool to "notify" on this object once its run method is done.

I run the risk though of the notify beign called before the wait, and then my thread would sleep forever.

Are there any other more elegant solutions?


I see wind mills
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40



Joining a thread in a thread pool doesn't make sense.

Traditionally, you want to join a thread, when you want to make sure that the task that is running on the thread has completed. In a thread pool, when the thread is done with the task, it will go back into a wait state, waiting for more tasks to run. There are some cases, where threads do terminate, depending on how the thread pool is configured -- but that is more related to the configuration than the tasks.


Not sure why you want to do this, but if the purpose is to wait for a task to be completed, would it not be easier to just use the task's future object? (assuming that you used one).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rodrigo Bossini
Ranch Hand

Joined: Jul 03, 2009
Posts: 113
Henry Wong wrote:

Joining a thread in a thread pool doesn't make sense.

Traditionally, you want to join a thread, when you want to make sure that the task that is running on the thread has completed. In a thread pool, when the thread is done with the task, it will go back into a wait state, waiting for more tasks to run. There are some cases, where threads do terminate, depending on how the thread pool is configured -- but that is more related to the configuration than the tasks.


Not sure why you want to do this, but if the purpose is to wait for a task to be completed, would it not be easier to just use the task's future object? (assuming that you used one).

Henry


I am implementing a parallel version of merge sort, following the pseudo-code from Cormen's Introduction to algorithms book.

I need the join behavior for implemeting it, and I want to use a pool because the pool is being used to simulate something like a computer cluster.

So, I would need to get a reference for the thread that the pool will use, and join that thread. Obviously, I want to join that thread when the task I gave it is done, and then it will go back to the pool.

I'm not using the Callable interface so there are no Future objects avaiable.
But I'll research a bit on this and see if it does what I need. Thanks for the tip.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

Joining a thread in the thread pool is definitely *not* what you want -- as the threads in a pool don't terminate based on the tasks. After the threads does the task it will go back into waiting for more tasks (or running another tasks). And since joining with a thread means waiting for termination, you will be waiting for much much longer than required, if not forever.

Using future *is* what you want. And you can get a future object on a runnable task.

Henry
Rodrigo Bossini
Ranch Hand

Joined: Jul 03, 2009
Posts: 113
Henry Wong wrote:Joining a thread in the thread pool is definitely *not* what you want -- as the threads in a pool don't terminate based on the tasks. After the threads does the task it will go back into waiting for more tasks (or running another tasks). And since joining with a thread means waiting for termination, you will be waiting for much much longer than required, if not forever.

Using future *is* what you want. And you can get a future object on a runnable task.

Henry


Yes, you're right. Thanks for the help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Join in a pool thread