Last active
February 15, 2017 13:28
-
-
Save rozaydin/e2de16e0251976c8163e60b5110f0033 to your computer and use it in GitHub Desktop.
Java Interrupts - Who or What clears interrupted flag and How ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by rozaydin on 2/15/17. | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
try { | |
t1 t1 = new t1(); | |
t1.start(); | |
Thread.sleep(10); | |
t1.interrupt(); | |
System.out.println("t1.isInterrupted: " + t1.isInterrupted() + " t1.state: " + t1.getState()); | |
Thread.sleep(20); | |
System.out.println("t1.isInterrupted: " + t1.isInterrupted() + " t1.state: " + t1.getState()); | |
} catch (Exception exc) { | |
System.out.println("Exeption: " + exc); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
Processing | |
Processing | |
Processing | |
t1.isInterrupted: true t1.state: RUNNABLE | |
Execution completed - interrupt status: true | |
t1.isInterrupted: false t1.state: TERMINATED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by rozaydin on 2/15/17. | |
*/ | |
public class t1 extends Thread { | |
@Override | |
public void run() { | |
while (!Thread.currentThread().isInterrupted()) { | |
System.out.println("Processing"); | |
} | |
System.out.println("Execution completed - interrupt status: " + Thread.currentThread().isInterrupted()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment