In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
本篇文章为大家展示了如何中断LockSupport线程,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
如何停止、中断一个运行中的线程??
线程api
1)面试题:如何使用中断标识停止线程?
在需要中断的线程中不断监听中断状态,一旦发生中断,就执行相应的中断处理业务逻辑。
1.修改状态
2.停止程序的运行
2)方法
1.通过一个volatile变量实现
package com.lyy.juc;import java.util.concurrent.TimeUnit;public class InterruptDemo {private static volatile boolean isStop = false;public static void main(String[] args) {new Thread(()->{while (true){if(isStop){System.out.println(Thread.currentThread().getName()+"线程-----isStop = true,自己退出");break; }System.out.println("-------hello interrupt"); } },"t1").start();try {TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }isStop = true; }}
主线程执行,入口,发现睡了一秒,睡这一秒里,变量isStop一致为false,打印 -------hello interrupt ,一秒过去,isStop为true,线程t1执行t1线程-----isStop = true,自己退出,break出循环
2.通过通过AtomicBoolean原子类
package com.lyy.juc;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicBoolean;public class StopThreadDemo {private static final AtomicBoolean atomicBoolean = new AtomicBoolean(true);public static void main(String[] args) {new Thread(()->{while(atomicBoolean.get()){try {//O.5s TimeUnit.MILLISECONDS.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); }System.out.println("-------hello"); } }).start();try {TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }atomicBoolean.set(false); }}
3通过Thread类自带的中断api方法实现
实例方法interrupt(),没有返回值
public void interrupt()
实例方法,
调用interrupt()方法仅仅是在当前线程中打了一个停止的标记,并不是真正立刻停止线程。
Example method isInterrupted, returns Boolean
public boolean isInterrupted()
example method,
Gets what the current value of the interrupt flag bit is,
Determine whether the current thread is interrupted (by checking the interrupt flag bit), default is false
代码
package com.lyy.juc;import java.util.concurrent.TimeUnit;public class InterruptDemo2{public static void main(String[] args) {Thread t1 = new Thread(() -> {while(true) {if(Thread.currentThread().isInterrupted()) {System.out.println("-----t1 线程被中断了,break,程序结束");break; }System.out.println("-----hello"); } }, "t1");t1.start();System.out.println("**************"+t1.isInterrupted());//暂停1毫秒 try { TimeUnit.MILLISECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }t1.interrupt();System.out.println("**************"+t1.isInterrupted()); }}
**************false
-----hello
若干-----hello
**************true
-----t1 线程被中断了,break,程序结束
4)当前线程的中断标识为true,是不是就立刻停止?
具体来说,当对一个线程,调用 interrupt() 时:
① 如果线程处于正常活动状态,那么会将该线程的中断标志设置为 true,仅此而已。
被设置中断标志的线程将继续正常运行,不受影响。所以, interrupt() 并不能真正的中断线程,需要被调用的线程自己进行配合才行。
② 如果线程处于被阻塞状态(例如处于sleep, wait, join 等状态),在别的线程中调用当前线程对象的interrupt方法,
那么线程将立即退出被阻塞状态,并抛出一个InterruptedException异常。
package com.lyy.juc;import java.util.concurrent.TimeUnit;public class InterruptDemo3 {public static void main(String[] args) throws InterruptedException {Thread t1 = new Thread(() -> {for (int i = 0; i < 3; i++) {System.out.println("-------" + i); }System.out.println("after t1.interrupt()--第2次---: " + Thread.currentThread().isInterrupted()); }, "t1");t1.start();System.out.println("before t1.interrupt()----: " + t1.isInterrupted());//实例方法interrupt()仅仅是设置线程的中断状态位设置为true,不会停止线程 t1.interrupt();//活动状态,t1线程还在执行中 try {TimeUnit.MILLISECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }System.out.println("after t1.interrupt()--第1次---: " + t1.isInterrupted());//非活动状态,t1线程不在执行中,已经结束执行了。 try {TimeUnit.MILLISECONDS.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }System.out.println("after t1.interrupt()--第3次---: " + t1.isInterrupted()); }}
循环次数不同,结果顺序不一样,不过最终状态改完后三个都为true
中断只是一种协同机制,修改中断标识位仅此而已,不是立刻stop打断
6)静态方法Thread.interrupted()
7)LockSupport
LockSupport是用来创建锁和其他同步类的基本线程阻塞原语。
下面这句话,后面详细说
LockSupport中的park() 和 unpark() 的作用分别是阻塞线程和解除阻塞线程
上述内容就是如何中断LockSupport线程,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.