线程中断的方法以及静态方法isInterrupted和实例方法interrupted的区别

Noella ·
更新时间:2024-09-20
· 557 次阅读

线程中断

常见的有以下两种方式:

通过共享的标记来进行沟通 调用 interrupt() 方法来通知

通过共享的标记来实现中断
就是创建一个boolean类型的变量来控制循环是否进行,就是一个标记。
代码如下:

/** * 描述:标记法中断线程 */ public class ThreadDemo { private static class MyRunnable implements Runnable { public volatile boolean isQuit = false; @Override public void run() { while (!isQuit) { //当标记为true时就中断了线程 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) throws InterruptedException { MyRunnable target = new MyRunnable(); Thread thread = new Thread(target, "1"); thread.start(); Thread.sleep(10 * 1000); target.isQuit = true;//让标记成为true } }

这个也是标记法实现的调用thread.interrupt()和Thread.interrupted()方法返回的是Boolean类型的数据,

/** * 描述:调用thread.interrupt()和Thread.interrupted()方法 */ public class Thread2 { private static class MyRunnable implements Runnable { @Override public void run() { // 两种方法均可以 while (!Thread.interrupted()) { // Thread.interrupted()返回true时就中断了线程 while (!Thread.currentThread().isInterrupted()) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); break; } } } } public static void main(String[] args) throws InterruptedException { MyRunnable target = new MyRunnable(); Thread thread = new Thread(target, "1"); thread.start(); Thread.sleep(10 * 1000); thread.interrupt();//调用thread.interrupt()也可以中断返回true } } }

调用 interrupt() 方法来通知

这种方式通知收到的更及时,即使线程正在 sleep 也可以马上收到

通过 thread 对象调用 interrupt() 方法通知该线程停止运行

thread 收到通知的方式有两种:
1). 如果线程调用了 wait/join/sleep 等方法而阻塞挂起,则以 InterruptedException 异常的形式通知,并清除中断标志
2). 只是内部的一个中断标志被设置,thread 可以通过

(1). Thread.interrupted() 判断当前线程的中断标志被设置,清除中断标志。 (2). Thread.currentThread().isInterrupted() 判断指定线程的中断标志被设置,不清除中断标志。

代码如下:
1,调用sleep方法产生异常,从而中断线程

/** * 描述:调用sleep方法产生异常,从而中断线程 */ public class ThreadInterrupt { private static class MyRunnable implements Runnable { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("通过异常收到了中断情况"); } for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().isInterrupted()); } } } public static void main(String[] args) throws InterruptedException { MyRunnable target = new MyRunnable(); Thread thread = new Thread(target, "1"); thread.start(); thread.interrupt(); } }

2,(1),调用Thread.interrupted()

/** * 描述:调用了Thread.interrupted()会标志位清除 */ public class ThreadDe { private static class MyRunnable implements Runnable { @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.interrupted()); } } } public static void main(String[] args) throws InterruptedException { MyRunnable target = new MyRunnable(); Thread thread = new Thread(target, "1"); thread.start(); thread.interrupt(); } }

调用了Thread.interrupted()会标志位清除
在这里插入图片描述2,调用Thread.currentThread().isInterrupted()

/** * 描述:调用Thread.currentThread().isInterrupted()标志位不会清除 */ public class ThreadDem { private static class MyRunnable implements Runnable { @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().isInterrupted()); } } } public static void main(String[] args) throws InterruptedException { MyRunnable target = new MyRunnable(); Thread thread = new Thread(target, "1"); thread.start(); thread.interrupt(); } }

调用Thread.currentThread().isInterrupted()标志位不会清除
在这里插入图片描述


作者:流年烟雨绽放在寂寞的晴天



方法 中断 静态 线程

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章