在上一篇博文中,介绍了成员方法interrupt()的用法,这篇接上上篇继续介绍剩下的两个与中断有关的方法。

2.成员方法new Thread().isInterrupted()

    通常使用Thread.currentThread().isInterrupted()方法判断某个线程是否已被发送过中断请求(某一线程被发送过中断请求后并一定会中断),因为它将线程中断标示位设置为true后,不会立刻清除中断标示位,即不会将中断标设置为false。程序示例如下:

package com.szw.test;

public class TestSun {
    public static void main(String[] args){
        // test1
        Thread t1 = new Thread(){
            @Override
            public void run(){
                try{
                    int i=0;
                    while(i++<100000000){
                        // nothing
                    }
                    System.out.println("A1");
                }catch(Exception e){
                    System.out.println("B1");
                    System.out.println(Thread.currentThread().isInterrupted() + " in catch");//抛出异常之后中断标志位才清除
                }
            };
        };
        t1.start();      
        System.out.println(t1.isInterrupted() + " before");//用来检查中断标志位的值
        t1.interrupt();//不中断正在运行的线程
        System.out.println(t1.isInterrupted() + " after");
    }
}
--------------
运行结果:
false before   //未调用interrupt()方法前,标志位为false
true after     //调用interrupt()方法之后标志位为true,  
A1             //线程t1调用了中断interrupt()方法,标准位也被设置为true,
               //但并没有中断,因为t1是正在运行的线程,interrupt()无法中断,
               //具体在上一篇《java中断小记(一)》中介绍


3.静态方法Thread.interrupted()

静态方法Thread.interrupted()来也可以用来获取中断状态,但在获取中断状态之后会清除中断状态(当然获取的是清除之前的值),也就是说连续两次调用此方法,第二次一定会返回false。程序示例:

package com.szw.test;

public class TestSun {
    public static void main(String[] args){
        // test1
        Thread t1 = new Thread(){
            @Override
            public void run(){
                try{
                    int i=0;
                    while(i++<100000000){
                        // nothing
                    }
                    System.out.println("A1");
                    System.out.println(Thread.interrupted() + " static");
                    System.out.println(Thread.interrupted() + " static 2");//第二次调用
                }catch(Exception e){
                    System.out.println("B1");
                    System.out.println(Thread.currentThread().isInterrupted() + " in catch");//抛出异常之后中断标志位才清除
                }
            };
        };
        t1.start();     
        System.out.println(t1.isInterrupted() + " before");
        t1.interrupt();//不中断正在运行的线程
        System.out.println(t1.isInterrupted() + " after");
    }
}
--------------------------------------------------
运行结果:
false before
true after
A1
true static
false static 2

-------------------------------
如果把t1.interrupt()注释掉,再次运行的结果为:
false before
false after
A1
false static            //第一次调用
false static 2          //第二次调用

到此关于中断的方法就告以段落了,最后贴上一道阿里的笔试题,可以分析下运行结果。。。

代码如下:

package com.szw.test;

public class TestThread{

    public static void main(String[] args){
        // test1
        Thread t1 = new Thread(){
            @Override
            public void run(){
                try{
                    int i=0;
                    while(i++<100000000){
                        // nothing
                    }
                    System.out.println("A1");
                }catch(Exception e){
                    System.out.println("B1");
                }
            };
        };
        t1.start();
        t1.interrupt();//不中断正在运行的线程
        
        // test2
        Thread t2 = new Thread(){
            public void run(){
                try{
                    Thread.sleep(5000);
                    System.out.println("A2");
                }catch(Exception e){
                    System.out.println("B2");
                }
            };
        };
        t2.start();
        t2.interrupt();    
        
        // test3
        Thread t3 = new Thread(){
            public void run(){
                try{
                    this.wait(50000);
                    System.out.println("A3");
                }catch(Exception e){
                    System.out.println("B3");
                }
            };
        };
        t3.start();
        t3.interrupt();    

        // test4
        Thread t4 = new Thread(){
            public void run(){
                try{
                    synchronized(this){
                        this.wait(50000);
                    }
                    System.out.println("A4");
                }catch(Exception e){
                    System.out.println("B4");
                }
            };
        };
        t4.start();
        t4.interrupt();    //可以中断是因为没有发生竞争上锁
        
        // test5
        try{
            t4.start();//Exception in thread "main" java.lang.IllegalThreadStateException
            System.out.println("A5");
        }catch(Exception e){
            System.out.println("B5");
        }
    }
}