线程中止的方法interrupt

    虽然线程方法里面有个方法为interrupt(),即线程中断,可是并非调用了线程中断,线程就真的中断了,若是线程在sleep中,则会被异常捕获,例子以下:    java

public static void main(String[] args) throws Exception {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    System.out.println(i);
                    i++;
                    if (Thread.currentThread().isInterrupted()) {
                        System.out.println("中断标识为真,我将中断等待");
                        break;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        System.out.println("我被中断异常捕获");
                    }
                }
            }
        });
        thread.start();
        Thread.sleep(3000);
        thread.interrupt();
        Thread.sleep(20 * 1000);
    }

    运行结果:ide

    能够看到,程序并无中断成功,反而被异常捕获,这时候,咱们能够想到,在异常里执行中断,该线程才会中断下来:线程

public static void main(String[] args) throws Exception {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    System.out.println(i);
                    i++;
                    if (Thread.currentThread().isInterrupted()) {
                        System.out.println("中断标识为真,我将中断等待");
                        break;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        System.out.println("我被中断异常捕获");
                        // 在异常里面增长中断方法
                        Thread.currentThread().interrupt();
                    }
                }
            }
        });
        thread.start();
        Thread.sleep(3000);
        thread.interrupt();
        Thread.sleep(20 * 1000);
    }

    运行结果:code

    能够看到这个时候才真正中断下来。因此使用中断的时候,不但主线程要执行中断,并且在执行线程的重点异常处,也要增长中断。另外,若是线程在执行循环,则必定要增长if (thread.isInterrupted()),不然线程就算在异常里面又一次执行了中断,循环仍是会继续执行,以下图:io

public static void main(String[] args) throws Exception {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    System.out.println(i);
                    i++;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        System.out.println("我被中断异常捕获");
                        // 在异常里面增长中断方法
                        Thread.currentThread().interrupt();
                    }
                }
            }
        });
        thread.start();
        Thread.sleep(3000);
        thread.interrupt();
        Thread.sleep(20 * 1000);
    }

    运行结果:class

    所以若是有循环要记得判断一下中断标识。中断标识一种是直接调用isInterrupted()方法判断,另外还能够经过在线程里面设置一个volatile变量,经过修改这个变量来实现。thread

相关文章
相关标签/搜索