| Refresh | Home EGTry.com

Thread.interrupted() clear the interrupted flag


Interrupted.java

/*
 * Copyright@ 2011 www.egtry.com
 */

package egtry.thread;

public class Interrupted extends Thread {

  public static void main(String[] args) {

    Interrupted thread1=new Interrupted();
    thread1.start();
    thread1.interrupt(); //send InterruptedException 

  }
  
  @Override
  public void run() {
		System.out.println("Thread started: ");
		for(int i=0; i<1000; i++) {
			System.out.println(i);
			if (Thread.interrupted()){
				System.out.println("Stopped");
				break;
			}
		}
		System.out.println("interrupted flag: "+Thread.interrupted()); //the flag was cleared
		System.out.println("Done");
  }
}



Output

Thread started: 
0
Stopped
interrupted flag: false
Done