The Thread class has an attribute that stores a boolean value indicating whether thethread has been interrupted or not. When you call the interrupt() method of a thread,you set that attribute to true. The isInterrupted() method only returns the value of that attribute.


There is an important difference between the isInterrupted() and the interrupted() methods. The first one doesn't change the value of the interrupted attribute, but the second one sets it to false. As the interrupted() method is a static method, the utilization of the isInterrupted() method is recommended.


If this thread is blocked in an invocation of the Object#wait(), Thread#join() Thread#sleep(long) , methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.


If this thread is blocked in an I/O operation upon an   java.nio.channels.InterruptibleChannel interruptible channel
then the channel will be closed, the thread's interrupt   status will be set, and the thread will receive a java.nio.channels.ClosedByInterruptException.
 
    
 If this thread is blocked in a {@link java.nio.channels.Selector} then the thread's interrupt status will be set and it will return
 immediately from the selection operation, possibly with a non-zero value, just as if the selector's java.nio.channels.Selector#wakeup wakeup method were invoked.


public class ThreadInterruptDemo1 {
	
	public static void main(String[] args) {
		Thread t = new Thread(new InterruptRunnable(),"t1");
		t.start();
		try {
			Thread.sleep(50l);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		t.interrupt();
	}
	
}

class InterruptRunnable implements Runnable{
	
	static AtomicInteger ato = new AtomicInteger(1);

	@Override
	public void run() {
		while(!Thread.currentThread().isInterrupted()){
			System.out.printf("print number is : %d \n",ato.getAndIncrement());
		}
		if(Thread.currentThread().isInterrupted()){
			System.out.printf("%s is interrupt.\n",Thread.currentThread().getName());
		}
	}
	
}

package com.csst.demo.interrupt;

import java.util.concurrent.atomic.AtomicInteger;

public class ThreadInterruptDemo2 {
	
	public static void main(String[] args) {
		Thread t = new Thread(new InterruptRunnable1(),"t2");
		t.start();
		try {
			Thread.sleep(50l);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		t.interrupt();
	}
	
}

class InterruptRunnable1 implements Runnable{
	
	static AtomicInteger ato = new AtomicInteger(1);

	@Override
	public void run() {
		System.out.printf("%s is enter run method. \n",Thread.currentThread().getName());
		try {
			Thread.sleep(1000l);
		} catch (InterruptedException e) {
			System.out.println("catched InterruptedException.");
			System.out.printf("is interrupt %s \n",Thread.currentThread().isInterrupted());
		}
	}
	
}