07.interrupt

/**
*isInterrupted
*/
public class InterruptDemo {
    public static void main(String[] args) throws InterruptedException{
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()){
                    System.out.println(Thread.currentThread()+"没被中断");
                }
            }
        });
        thread.start();
        Thread.sleep(1000);
        System.out.println("main thread interrupt thread");
        thread.interrupt();
        thread.join();
        System.out.println("main is over");
        //...
        //Thread[Thread-0,5,main]没被中断
        //Thread[Thread-0,5,main]没被中断
        //Thread[Thread-0,5,main]没被中断
        //Thread[Thread-0,5,main]没被中断
        //Thread[Thread-0,5,main]没被中断
        //main thread interrupt thread
        //Thread[Thread-0,5,main]没被中断
        //main is over
    }
}
/**
* interrupt()方法打断线程的休眠
*/
public class InterruptDemo2 {
    public static void main(String[] args) throws InterruptedException{
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("threadOne begin sleep for 200 s");
                    Thread.sleep(200000);
                    System.out.println("threadOne waking");
                } catch (InterruptedException e) {
                    System.out.println("threadOne is interrupted while sleeping");
                    return;
                }
                System.out.println("threadOne-leaving normally");
            }
        });
        threadOne.start();
        //确保子线程进入休眠状态
        Thread.sleep(1000);
        //打断子线程的休眠,让子线程从sleep函数返回
        threadOne.interrupt();
        //等待子线程执行完毕
        threadOne.join();
        System.out.println("main thread is over");
        //threadOne begin sleep for 200 s
        //threadOne is interrupted while sleeping
        //main thread is over
        // threadOne 线程休眠了200s,在正常情况下该线程需要等到 200s 后才会被唤醒,
        // 但是通过调用 threadOne.interrupt()方法打断了该线程的休眠,
        // 该线程会在调用 sleep 方法处抛出 InterruptedException 异常后返回

    }
}
/**
 *  interrupted() 与 isInterrupted()
 */
public class InterruptDemo3 {
    public static void main(String[] args) throws InterruptedException{
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){}
            }
        });
        threadOne.start();
        threadOne.interrupt();
        System.out.println("isInterrupted:"+threadOne.isInterrupted());
        System.out.println("isInterrupted:"+threadOne.interrupted());
        System.out.println("isInterrupted:"+Thread.interrupted());
        System.out.println("isInterrupted:"+threadOne.isInterrupted());
        threadOne.join();
        System.out.println("main thread is over");
        //isInterrupted:true
        //isInterrupted:false
        //isInterrupted:false
        //isInterrupted:true
        // 在 interrupted()内部是获取当前调用线程的中断标志而不是调用 interrupted()方法的实例对象的中断标志。
        //  public static boolean interrupted() {
        //        return currentThread().isInterrupted(true);
        //    }
        //在 interrupted()方法内部是获取当前线程的中断状态,这里虽然调用了 threadOne 的 interrupted() 方法,
        //但是获取的是主线程的中断标志,因为主线程是 当前线程。 threadOne.interrupted()和 Thread.interrupted()方法的作用是一样的,
        // 目的都是 获取当前线程的中断标志
    }
}
/**
* interrupted()
*/
public class InterruptDemo4 {
    public static void main(String[] args) throws InterruptedException{
        Thread threadOne = new Thread(new Runnable(){
            @Override
            public void run() {
                //中断标志为true时会退出循环,并清除中断标志
                while (!Thread.currentThread().interrupted()){}
                System.out.println("threadOne interrupted:"+Thread.currentThread().isInterrupted());
            }
        });
        threadOne.start();
        threadOne.interrupt();
        threadOne.join();
        System.out.println("main thread is over");
        //threadOne interrupted:false
        //main thread is over
    }
}

おすすめ

転載: www.cnblogs.com/fly-book/p/11362505.html