• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Join in a pool thread

 
Rodrigo Bossini
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Rodrigo Bossini
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic