Forums Register Login

Join in a pool thread

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send


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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
Their achilles heel is the noogie! Give them noogies tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2320 times.
Similar Threads
After notify()
Thread
wait x join
Thread methods
Notify()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:02:06.