Reputation: 517
i've got an application with a possible number of threads. basicly the threads should work this:
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
Upvotes: 0
Views: 165
Reputation: 200158
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.
Upvotes: 1