JavaWeb——线程的中断

线程中断

1、任务执行完毕结束

让线程的run执行完
在这里插入图片描述
结束方式比较温和,当标记位被设置上之后,等到,当前这次循环执行完了之后,再结束线程.

public class ThreadDemo1 {
    private static boolean isQuit = false;
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(){
            @Override
            public void run() {
                while ((!isQuit)) {
                    System.out.println("别烦我,在转账");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("转账操作被终止");
            }
        };
        t.start();
        Thread.sleep(5000);
        System.out.println("终止交易");
        isQuit = true;
    }
}

例如当线程执行到sleep的时候,已经sleep 100ms了.此时isQuit被设置为true,当前线程不会立刻退出,而是会继续sleep,把剩下400ms sleep完,才会结束线程.

2、任务执行一半,强制结束

调用线程的interrupt方法(比较激烈)
在这里插入图片描述
在这里插入图片描述

public class ThreadDemo2 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(){
            @Override
            public void run() {
                //直接使用线程内部的标记位进行判定
                while (! Thread.currentThread().isInterrupted()) {
                    System.out.println("别烦我,在转账");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
                System.out.println("转账操作被终止");
            }
        };
        t.start();
        Thread.sleep(5000);
        System.out.println("停止");
        t.interrupt();//强制中断
    }
}

更优先考虑第一种(比较温和的方式)更容易保证原子性.

中断标记

重点说明下第二种方法:
通过 thread 对象调用 interrupt() 方法通知该线程停止运行
thread 收到通知的方式有两种:

  1. 如果线程调用了 wait/join/sleep 等方法而阻塞挂起,则以 InterruptedException 异常的形式通知,清除中断标志 ,
  2. 否则,只是内部的一个中断标志被设置,thread 可以通过
    Thread.interrupted() 判断当前线程的中断标志被设置,清除中断标志
    Thread.currentThread().isInterrupted() 判断指定线程的中断标志被设置,不清除中断标志

中断标记可以是一个boolearn值,初始值为false
调用interrupt,此时就把这个中断标记设为true
Thread.interrupted()来判定中断标记,返回结果为true,同时会把中断标记设回false(结束闹钟)
Thread.currentThread0.isInterrupted()判定中断标记,返回结果一直是true(暂停闹钟)

t.interrupt 被调用的时候,Thread.interrupted()就能感知到,感知一次之后,标记位就被清除了

public class ThreadDemo3 {
    public static void main(String[] args) {
        Thread t = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println(Thread.interrupted());


                }
            }
        };
        t.start();

        t.interrupt();
    }
}

true
false
false
false
false
false
false
false
false
false

仅仅只是判定标记位,而不会修改标记位

public class ThreadDemo3 {
    public static void main(String[] args) {
        Thread t = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println(Thread.currentThread().isInterrupted());


                }
            }
        };
        t.start();

        t.interrupt();
    }
}

true
true
true
true
true
true
true
true
true
true

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值