package com.example.app;

import java.util.concurrent.locks.Lock;

public class TestInterrupt {
public static void main(String[] args) {

Thread t1=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("in thread 1......");

while(true){
if(Thread.currentThread().isInterrupted()){
System.out.println("it was interrupted and will exit!");
break;
}
}
}
});
t1.start();

try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}

t1.interrupt();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finished......");

}
}

总结:
1.调用interrupt方法只是设置线程的中断标志为true。被interrupt的线程自己来决定后续动作,不一定会中断线程的运行
2.线程可以通过isInterrupted()方法来检查自己的中断标记来决定后续动作