简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM