Java多线程学习笔记(二)

一、interrupt() 方法

        (1)thread.interrupt(),该方法用于中断Thread线程(此线程并非当前运行线程),而是调用interrupt()方法的实例所代表的线程。

        (2)thread.interrupt(),该方法并不能真正中断线程,而是将调用interrupt()方法的实例所代表的线程中断标记位设置为true。

二、isInterrupted() 方法

        (1)thread.isInterrupted(),该方法用于测试调用isInterrupted()方法的实例所代表的线程是否为处于中断状态。

        (2)thread.isInterrupted(),该方法不具备清除线程中断状态的功能。

@Test
public void test_interrupt() {
        MyInterruptThread myInterruptThread = new MyInterruptThread("my-thread");
        myInterruptThread.start();
        myInterruptThread.interrupt();
        System.out.println("myInterruptThread.isInterrupted() :" + myInterruptThread.isInterrupted());
        System.out.println("myInterruptThread.isInterrupted() :" + myInterruptThread.isInterrupted());
        System.out.println("myInterruptThread.isAlive():" + myInterruptThread.isAlive());
        System.out.println("main.isInterrupted() :" + Thread.currentThread().isInterrupted());
    }


运行结果:
myInterruptThread.isInterrupted() :true
myInterruptThread.isInterrupted() :true
myInterruptThread.isAlive():true
main.isInterrupted() :false

结果分析:
        (1)myInterruptThread.isInterrupted() 结果为 true, main.isInterrupted() 结果为 false,则证明 myInterruptThread.interrupt()方法是对 myInterruptThread 实例所对应线程进行的中断操作。
        (2)myInterruptThread.isInterrupted() 执行了两次均返回true,则说明 isInterrupted() 方法仅仅返回 myInterruptThread 线程的中断状态,不清除中断状态。
        (3)myInterruptThread.isAlive() 结果为true,则证明 interrupt() 并不会直接中断线程,而仅仅是将中断标记位设置为true。

三、interrupted() 方法

        (1)Thread.interrupted(),该方法为Thread类的static方法,测试当前线程是否处于中断状态,执行后将线程的中断状态设置为false。

        Thread.interrupted() 的定义

public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }
@Test
public void test_interrupt2() {
    Thread.currentThread().interrupt();
    System.out.println("执行前-main.isInterrupted():" + Thread.currentThread().isInterrupted());
    System.out.println("执行前-main.isInterrupted():" + Thread.currentThread().isInterrupted());
    System.out.println("main interrupted():" + Thread.interrupted());
    System.out.println("main interrupted():" + Thread.interrupted());
    System.out.println("执行后-main.isInterrupted():" + Thread.currentThread().isInterrupted());
}

执行结果:
   执行前-main.isInterrupted():true
   执行前-main.isInterrupted():true
   main interrupted():true
   main interrupted():false
   执行后-main.isInterrupted():false

结果分析:

        (1)两次 “执行前-main.isInterrupted()”结果均为 “true”,说明 isInterrupted() 只是查询调用线程的中断标志,并不会在调用isInterrupted()后清除中断标志状态。

        (2)两次 “main interrupted()”结果不一致,第一次为“true”,第二次为“false”,说明 interrupted()返回当前线程中断标志状态的同时,也会清除线程的中断标志状态。

@Test
public void test_interrupted_thread() throws Exception {
    InterruptThread myThread = new InterruptThread();
    myThread.start();
    myThread.interrupt();  // 中断myThread线程
    System.out.println("【第一次】线程是否处于中断状态 =" + myThread.interrupted());
    Thread.currentThread().interrupt();  // 中断当前Main线程
    System.out.println("【第二次】线程是否处于中断状态 =" + myThread.interrupted());
    System.out.println("【第三次】线程是否处于中断状态 =" + myThread.interrupted());
}


执行结果:
【第一次】线程是否处于中断状态 =false
【第二次】线程是否处于中断状态 =true
【第三次】线程是否处于中断状态 =false

结果分析:

        (1)Main线程调用 myThread.interrupt()使得 myThread 线程中断标志位true,但“【第一次】线程是否处于中断状态”结果为“false”,说明 myThread.interrupted() 测试的不是myThread线程是否处于中断状态,而是测试当前调用线程(即Main)的中断状态。

        (2)Thread.currentThread().interrupt();,当Main线程对自己进行中断后,“【第二次】线程是否处于中断状态 ”结果为“true”,再一次说明myThread.interrupted() 测试的是当前调用线程(即Main)的中断状态,并且第三次结果为“false”,说明interrupted()会清除线程的中断标志状态。

四、当抛出InterruptedException异常时,中断标记被重置为false

@Test
public void test_interrupt4() throws InterruptedException {
    MyInterruptThread3 myThread3 = new MyInterruptThread3("myThread3");
    myThread3.start();
    Thread.sleep(500);
    myThread3.interrupt();
    myThread3.join();
}

public class MyInterruptThread3 extends Thread {

    public MyInterruptThread3(String name) {
        super(name);
    }

    @Override
    public void run() {
        Thread currentThread = Thread.currentThread();
        while (!currentThread.isInterrupted()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("抛出异常," + currentThread.getName() + " interrupt flag is " +
                        currentThread.isInterrupted());
                // 当线程抛出 InterruptedException 异常时,会重置中断标记位为false,需再次调用interrupt()方法才可以退出循环
                currentThread.interrupt();
            }
            System.out.println(currentThread.getName() + "运行中...");
        }
        System.out.println("跳出循环后" + currentThread.getName() + " interrupt flag is " + currentThread.isInterrupted());
    }
}

执行结果:
myThread3运行中...
myThread3运行中...
myThread3运行中...
myThread3运行中...
java.lang.InterruptedException: sleep interrupted
	at java.lang.Thread.sleep(Native Method)
	at com.demo.a.a5.MyInterruptThread3.run(MyInterruptThread3.java:19)
抛出异常,myThread3 interrupt flag is false
myThread3运行中...
跳出循环后myThread3 interrupt flag is true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值