线程中断测试

package com.fh.interview;

/**
 * @author
 * @create 2018-06-12 下午10:15
 **/
public class Interrent {

    public static void main(String[] args) {
        Thread run = new Thread(new Runnable() {
            @Override
            public void run() {
                int i=0;
                while (true){
//                    System.out.println("running");
                    i++;
                }
            }
        });
        Thread sleep = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(100000);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        run.start();
        sleep.start();
        run.interrupt();
        sleep.interrupt();
        System.out.println("*************");
        System.out.println("run state:"+run.isInterrupted());
        System.out.println("**************");
        System.out.println("sleep state:"+sleep.isInterrupted());
        System.out.println("***************");
        System.out.println("run state:"+run.isAlive());
        System.out.println("****************");
        System.out.println("sleep state:"+sleep.isAlive());

       /* *************
        run state:true
                **************
        sleep state:true
                ***************
        run state:true
                ****************
        sleep state:true
        正在运行的任务调用interrupt,并不会停止线程的运行
        处于sleep,join,wait的线程,会抛出中断异常
        */


    }
}

猜你喜欢

转载自www.cnblogs.com/nihaofenghao/p/9175465.html