需要检测isInterrupted()标志,其他线程通过调用interrupt()方法中断该线程
class HelloThread extends Thread{
    public void run(){
        while(!isInterrupted()){
            System.out.println("Hello");
        }
    }
}
class Main{
    public static void main(String[] args) throws Exception{
        Thread t = new HelloThread();
        t.start();
        Thread.sleep(1000);
        t.interrupt();
    }
}如果线程处于等待状态,该线程会捕获InterruptedException。捕获到InterruptedException说明有其他线程对其调用了interrupt()方法,通常情况下该线程应该立即结束运行。
class HelloThread extends Thread{
    public void run(){
        while(!isInterrupted()){
            System.out.println("Hello");
            try{
                Thread.sleep(100);
            }catch (InterruptedException e){
                e.printStackTrace();
                return;
            }
        }
    }
}class HelloThread extends Thread{
    public volatile boolean running = true;
    public void run(){
        while(running){
            System.out.println("Hello"); 
        }
    }
}
class Main{
    public static void main(String[] args) throws Exception{
        HelloThread t = new HelloThread();
        t.start();
        Thread.sleep(1000);
        t.running = false;
    }
}线程间共享变量需要使用volatile关键字标记,确保线程能读取到更新后的变量值

Java虚拟机中,变量的值保存在主内存中,但是当线程访问一个变量的时候,会先获取一个副本,并且保存自己的工作内存中。如果线程修改变量的值,虚拟机会在某个时刻把修改后的值回写到主内存,但是这个时间是不确定的。这会导致线程更新了某个变量,另一个线程读取的变量还是更新之前的。

如主内存中a的值是true,线程1将a的值改写为false,虚拟机将线程1修改后的值回写到主内存的时间是不确定的,执行线程2时,线程2读取的值可能仍然是true,而不是线程1更新后的false。
volatile关键字的目的是告诉虚拟机:
volatile关键字解决的是可见性问题:
class InterrptThread extends Thread{
    public void run(){
        while(!isInterrupted()){
            System.out.println("Hello");
            try{
                Thread.sleep(100);
            }catch (InterruptedException ex){
                System.out.println("Interrupted!");
                break;
            }
        }
        System.out.println("Thread end");
    }
}
public class InterruptTest {
    public static void main(String[] args) throws InterruptedException{
        Thread t = new InterrptThread();
        t.start();
        Thread.sleep(1000);
        t.interrupt();
        System.out.println("main end");
    }
}
class InterrptThread extends Thread{
    volatile boolean running = true;
    public void run(){
        while(running){
            System.out.println("Hello");
            try{
                Thread.sleep(100);
            }catch (InterruptedException ex){
                System.out.println("Interrupted!");
                break;
            }
        }
        System.out.println("Thread end");
    }
}
public class InterruptTest {
    public static void main(String[] args) throws InterruptedException{
        InterrptThread t = new InterrptThread();
        t.start();
        Thread.sleep(1000);
        t.running = false;
        System.out.println("main end");
    }
}
原文:https://www.cnblogs.com/csj2018/p/10994626.html