java - Stopping a thread forcefully -
suppose have following piece of code running thread:
public void logic() { //step1 //step2 //step3 //step4 ... ... //stepn }
nowhere in method interruptible methods called(like wait, sleep, join).and steps taking lot of times execute. reason might other blocking operations (socket.read or read methods of file). , want ensure if entire logic method not executed in m seconds thread should killed. best way achieve this?
you "nowhere in method interruptible methods called(like wait, sleep, join)", interruption isn't limited blocking methods; method can respond interruption if check it.
put between each step in logic
method:
if (thread.currentthread().interrupted()) { throw new interruptedexception(); }
(you might want put static allowinterruption
method somewhere , call after each step.)
Comments
Post a Comment