user2448754
user2448754

Reputation: 51

Select a thread in threadpool to shutdown

I currently have a threadpool with 2 fixed threads and each thread creates 2 more threads that perform task. I have it set up to where I can pass commands to stop a thread if needed.

What I'm asking is if there is a way to select a specific fixed thread from the threadpool and shut it down.

I have everything set up to shutdown the thread just need a way to select one of the two threads and shut it down and have the other one continue running.

If there is a better way to do this I'm open to other options.

Thanks

Upvotes: 0

Views: 269

Answers (1)

Gray
Gray

Reputation: 116918

What I'm asking is if there is a way to select a specific fixed thread from the threadpool and shut it down.

Not from the pool itself, no. Remember that you don't want to kill the thread in a thread-pool since there may be more tasks to execute.

If there is a better way to do this I'm open to other options.

I'd have a volatile boolean that is being checked in the task in question so you can cause it to quit.

private volatile boolean shutdownSpecificTask;
...
// then inside of your task you'd do something like
while (!shutdownSpecificTask) {
   ...
}

The only operations like this that you have at the thread-pool level is to interrupt all of the running threads with a shutdownNow() or a Future.cancel(true). Both of these interrupt the thread which sets the interrupt flag and cause methods that throw InterruptedException to do so.

Upvotes: 1

Related Questions