Java 学习笔记之 线程interrupt方法

线程interrupt方法:

interrupt方法是用来停止线程的,但是他的使用效果并不像for+break那样,马上就停止循环。

调用interrupt()其实仅仅是在当前线程中打了一个停止标记,并没有真正的停止线程。

在下面的例子中,可以看出在interrupt()方法后的语句仍然被执行了。

public class ThreadRunMain {
    public static void main(String[] args) {
        testMainInterruptThread();
    }

    public static void testMainInterruptThread(){
        Thread.currentThread().interrupt();
        System.out.println("First call: " + Thread.interrupted());
        System.out.println("Second call: " + Thread.interrupted());
        System.out.println("end!");
    }
}

运行结果:

原文地址:https://www.cnblogs.com/AK47Sonic/p/7668000.html