Skip to content

Instantly share code, notes, and snippets.

@rozaydin
Last active February 15, 2017 13:28
Show Gist options
  • Save rozaydin/e2de16e0251976c8163e60b5110f0033 to your computer and use it in GitHub Desktop.
Save rozaydin/e2de16e0251976c8163e60b5110f0033 to your computer and use it in GitHub Desktop.
Java Interrupts - Who or What clears interrupted flag and How ?
/**
* 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);
}
}
}
...
Processing
Processing
Processing
t1.isInterrupted: true t1.state: RUNNABLE
Execution completed - interrupt status: true
t1.isInterrupted: false t1.state: TERMINATED
/**
* 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