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:

  1. if interruption comes during execution of dosomelongjob, method continue natural end? (i suppose yes long no-one checks thread.interrupted() i'd confirm);
  2. after catching interruption, interrupted() status reset false?
  3. 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:

  1. when interruption mean interruptedexception? if so, think should move dosomelongjob inside try handle possible exception.

  2. it not reset false, have manually.

  3. to reset interrupted status, please call thread.interrupted() .


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -