java之interrupt中断线程

java中线程在运行过程当中能够经过interrupt方法进行中断,这里须要提到几个的注意点:java

一、中断状态是能够被清除或者说恢复的
二、中断请求不是必定会被响应(如io包中的一些操做,只会标记中断状态,而对线程并无实际影响)
三、调用interrupt并非当即中断线程执行,而是传递了中断请求

下面看demoide

public class MyInterrupt implements Runnable{


    private volatile int i = 0;
    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {

            System.out.println(i++ + String.valueOf(Thread.currentThread().isInterrupted()));
            
            //Point1:若是在这里调用中断请求,程序会在i=20时抛出异常,但不会中断,线程继续执行下去
            //if (i == 20) {
            //    cancel();
            //}
            try {
                Thread.sleep(100);
                //this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();

            }
            //Point2:在此处调用中断请求,最后打印 19:false  isInterrupted()返回true因此不在进入循环体
            if (i == 20) {
                cancel();
            }

        }
    }

    public void cancel(){
        Thread.currentThread().interrupt();
    }

    public static void main(String[] args) {
        new Thread(new MyInterrupt()).start();
    }
}

在point1处去中断时候为何程序会继续执行,这里咱们看下sleep方法以及wait方法的docthis

/**
     * Causes the currently executing thread to sleep (temporarily cease
     * execution) for the specified number of milliseconds, subject to
     * the precision and accuracy of system timers and schedulers. The thread
     * does not lose ownership of any monitors.
     *
     * @param  millis
     *         the length of time to sleep in milliseconds
     *
     * @throws  IllegalArgumentException
     *          if the value of {@code millis} is negative
     *
     * @throws  InterruptedException
     *          if any thread has interrupted the current thread. The
     *          <i>interrupted status</i> of the current thread is
     *          cleared when this exception is thrown.
     */
    public static native void sleep(long millis) throws InterruptedException;

在jdk8中的注释中咱们能够发现@throws中说明了,若是任意线程发出了中断请求,当抛出InterruptedException异常后,中断状态会被清除,也就是说该线程不会被中断,再次调用isInterrupted方法则会返回false。线程

相关文章
相关标签/搜索