java - "Clean" way to interrupt a thread which could be doing something -
ok, let's have thread executes while() loop. during loop, , go sleep:
public void run() { while (some_condition) { dosomelongjob(); // long job try { thread.sleep(a_bit); // sleep } catch (interruptedexception ie) { handleinterruptionbutdonotexitloop(); } } }
some questions:
- if interruption comes during execution of dosomelongjob, method continue natural end? (i suppose yes long no-one checks thread.interrupted() i'd confirm);
- after catching interruption, interrupted() status reset false?
- in conclusion, thread can interrupted n times long thread status reset? there way reset manually thread status in order handle interruption during execution of dosomelongjob?
to answer questions:
when interruption mean interruptedexception? if so, think should move dosomelongjob inside try handle possible exception.
it not reset false, have manually.
to reset interrupted status, please call thread.interrupted() .
Comments
Post a Comment