InterruptDemo.java 525 字节
Newer Older
L
summit  
liulei33 已提交
1
package com.Mrliu.thread.base;
L
liulei33 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

import java.util.concurrent.TimeUnit;

/**
 * 线程中断演示thread.interrupt
 */
public class InterruptDemo {

    private static int i;

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
           while (!Thread.currentThread().isInterrupted()){
               i++;
           }
            System.out.println(i);
        });

        thread.start();

        TimeUnit.SECONDS.sleep(1);
        thread.interrupt();
    }
}