温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何中断LockSupport线程

发布时间:2021-09-27 10:02:37 来源:亿速云 阅读:113 作者:柒染 栏目:编程语言

本篇文章为大家展示了如何中断LockSupport线程,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

如何停止、中断一个运行中的线程??

线程api

如何中断LockSupport线程

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;
    }
}

如何中断LockSupport线程

主线程执行,入口,发现睡了一秒,睡这一秒里,变量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);
    }
}

如何中断LockSupport线程

3通过Thread类自带的中断api方法实现

实例方法interrupt(),没有返回值

public void interrupt()

实例方法,
调用interrupt()方法仅仅是在当前线程中打了一个停止的标记,并不是真正立刻停止线程。


如何中断LockSupport线程

实例方法isInterrupted,返回布尔值

public boolean isInterrupted()

实例方法,
获取中断标志位的当前值是什么,
判断当前线程是否被中断(通过检查中断标志位),默认是false

如何中断LockSupport线程

代码

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());
    }
}

如何中断LockSupport线程

循环次数不同,结果顺序不一样,不过最终状态改完后三个都为true

中断只是一种协同机制,修改中断标识位仅此而已,不是立刻stop打断

如何中断LockSupport线程

6)静态方法Thread.interrupted()

7)LockSupport

LockSupport是用来创建锁和其他同步类的基本线程阻塞原语。
 
下面这句话,后面详细说
LockSupport中的park() 和 unpark() 的作用分别是阻塞线程和解除阻塞线程
 

上述内容就是如何中断LockSupport线程,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI