JAVA笔记概览

来源:互联网 发布:域名查询软件西部数码 编辑:程序博客网 时间:2024/05/17 02:33

相关方法

  1. interrupt()
  2. isInterrupted()

第一种情况

public class Test {    public static void main(String[] args) {        Thread t = new T();        t.start();        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        t.interrupt();    }}class T extends Thread {    @Override    public void run() {        int count = 0;        while (true) {            System.out.println("--- " + count + " ---");            if (Thread.currentThread().isInterrupted()) {                System.out.println("Thread has interrupted");                break;            }            ++count;        }    }}

执行结果: 正常中断

....--- 394930 ------ 394931 ------ 394932 ---Thread has interrupted

第二种情况

public class Test {    public static void main(String[] args) {        Thread t = new T();        t.start();        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        t.interrupt();    }}class T extends Thread {    @Override    public void run() {        int count = 0;        while (true) {            try {                Thread.sleep(100);            } catch (InterruptedException e) {                e.printStackTrace();            }            System.out.println("--- " + count + " ---");            if (Thread.currentThread().isInterrupted()) {                System.out.println("Thread has interrupted");                break;            }            ++count;        }    }}

执行结果: 线程没有中断, 中间抛出了个 异常 但是 继续执行

..--- 8 ------ 9 ---java.lang.InterruptedException: sleep interrupted    at java.lang.Thread.sleep(Native Method)    at aaa.T.run(Test.java:30)--- 10 ------ 11 ---..

原理分析:
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
-> 如果 遇到 sleep(或其他) 解除中断, 并抛出 InterruptedException 异常.

第二种情况解决

思路:
1. 既然状态会被清楚, 咱就不用原来的状态(咱也看不到) 自己定义一个状态
2. 在抛异常的时候 中断状态 改为 true

public class Test {    public static void main(String[] args) {        Thread t = new T();        t.start();        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        t.interrupt();    }}class T extends Thread {    // 定义一个 标记 表示 是否 中断    private static boolean status = false;    @Override    public void run() {        int count = 0;        while (true) {            try {                Thread.sleep(100);            } catch (InterruptedException e) {                // 抛出异常的同时 改变 状态                status = true;                // 这里就不捕获异常了                // e.printStackTrace();            }            System.out.println("--- " + count + " ---");            // 使用自定义中断状态判断            if (status) {                System.out.println("Thread has interrupted");                break;            }            ++count;        }    }}
原创粉丝点击