简体   繁体   中英

Terminate Thread in Threadpool

i've got an application with a possible number of threads. basicly the threads should work this:

  • Main Thread
  • CalculationThread
  • CalculationThread
  • CalculationThread

Adding / Executing those threads to a FixedThreadPool isn't the problem. The Thread itself calls a certain function in the Mainthread to submit the results. After this step, the thread should sleep until it will be called again for the next calucation.

The Mainthread holds a reference to a CalculationThread to submit updates to the thread and readd it to the pool to start the next calculation.

My Problem: How can I enforce a timeout for a certain thread? The enforcement of this timeout must also work, if a endless loop occurs

You cannot enforce the timeout without cooperation from the thread, at least not in a sane way. You should code your calculation tasks so that they comply with the Java interruption mechanism. Basically, that means occasionally checking the Thread.interrupted return value and aborting on true .

The only other option is the ham-handed – and deprecated – Thread.stop , which can wreak general chaos, especially when done on a pool-managed thread.

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