【JUC】08-中断机制之中断协商机制

1. 中断协商机制

一个线程应该自己主动去中断。所以Thread.stop, Thread.suspend, Thread.resume都已经被废弃。其他线程设置中断标识位为true,本线程会自行决定是否需要结束本线程。

  • void interrupt()设置线程的中断状态为true,发起一个协商而不会立刻停止线程。
  • static boolean interrupted()判断线程是否被中断并清除当前中断状态:1. 返回当前线程的中断状态,测试当前线程是否已被中断; 2. 将当前线程的中断状态清零并重新设为false,清除线程的中断状态。
  • boolean isInterrupted()测试此线程是否已被中断。
public class SLEEPThread {
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("exit ..." + Thread.currentThread().isInterrupted());
                    break;
                }
                try {
                    /* 其他线程将中断标识位设置为true时,sleep方法会抛出InterruptedException,
                     * 中断标识也被清空置为 false.
                     */
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    // 再次将 interrupted 设置为 false
                    Thread.currentThread().interrupt();
                    e.printStackTrace();
                }
                System.out.println("This is thread 1. Interrputed is " + Thread.currentThread().isInterrupted());
            }

        }, "t1");
        t1.start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(()->{
            t1.interrupt();
        }, "t2").start();
    }
}

public class InterruptedDemo {
    public static void main(String[] args) {
        Thread.currentThread().interrupt();

        // true
        System.out.println(Thread.currentThread().isInterrupted());
        // true
        System.out.println(Thread.currentThread().isInterrupted());

        // true, 先返回真实值, 再将中断标识位设为false
        System.out.println(Thread.interrupted());
        // false
        System.out.println(Thread.interrupted());
    }
}

2. volatile实现线程中断

public class InterruptDemo {
    // volatile实现线程中断
    static volatile boolean isStop = false;
    public static void main(String[] args) {
        new Thread(()->{
            while (true) {
                if (isStop) {
                    System.out.println("interrupt...");
                    break;
                }
                System.out.println("Hello volatile.");
            }
        }, "t1").start();
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            isStop = true;
        },"t2").start();
    }
}

3. AtomicBoolean实现中断协商

public class AtomicBooleanDemo {
    static AtomicBoolean atomicBoolean = new AtomicBoolean(false);
    public static void main(String[] args) {
        new Thread(()->{
            while (true) {
                if (atomicBoolean.get()) {
                    System.out.println("AtomicBoolean exit ...");
                    break;
                }
                System.out.println("Hello Atomic ...");
            }
        }, "t1").start();
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            atomicBoolean.set(true);
        },"t2").start();
    }
}

4. Thread API实现协商中断

public class ThreadInterruptDemo {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            /*
            当前线程处于阻塞状态(例如sleep、wait和join等状态),
            别的线程调用当前线程的interrupt方法,当前线程会退出阻塞,并抛出InterruptedException异常。
            */
            try {
                TimeUnit.SECONDS.sleep(6);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("eixt ...");
                    break;
                }
                System.out.println("Hello");
            }
        }, "t1");
        t1.start();
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            t1.interrupt();
        },"t2").start();
    }
}
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CRE_MO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值