当前位置:网站首页>详解LockSupport的使用
详解LockSupport的使用
2022-07-01 21:43:00 【键盘歌唱家】
一、简介
1.下图是Java api中的描述内容:
2.它的方法有以下几个,其中被圈出来的方法是核心方法:
3.唤醒和阻塞线程的常用方法中包含了LockSupport:
二、LockSupport唤醒阻塞方法的优势
1.Object类提供的wait
和notify
需要在synchronized代码块中使用,否则会报IllegalMonitorStateException
异常 !并且notify
必须在wait方法后面执行才能够生效 。(下面是正确的用法!)
public static void main(String[] args) {
Object objectlock = new Object();
new Thread(()->{
synchronized (objectlock){
System.out.println(Thread.currentThread().getName()+":\tcome in~");
try {
objectlock.wait();} catch (InterruptedException e) {
e.printStackTrace();}
System.out.println(Thread.currentThread().getName()+": \t被唤醒");
}
},"A").start();
//休眠1秒
try {
TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {
e.printStackTrace();}
new Thread(()->{
synchronized (objectlock) {
objectlock.notify();
System.out.println(Thread.currentThread().getName()+": \t发出通知!");
}
},"B").start();
}
2.ReentrantLock类提供的await
和signal
依旧存在执行顺序的问题,下面的案例让signal方法先执行,结果发现后执行的await并没有被唤醒!
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
new Thread(()->{
//暂停1秒,让B线程先执行!
try {
TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {
e.printStackTrace();}
lock.lock();
try {
System.out.println(Thread.currentThread().getName()+":\tcome in~");
condition.await();
System.out.println(Thread.currentThread().getName()+": \t被唤醒");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
},"A").start();
new Thread(()->{
lock.lock();
try {
condition.signal();
System.out.println(Thread.currentThread().getName()+": \t发出通知!");
}finally {
lock.unlock();
}
},"B").start();
}
3.LockSupport就不存在上面的问题,类似高速公路的ETC,提前买好了通行证unpark
,到闸机处直接抬起栏杆放行了没有park
拦截了。但unpark重复执行也只能积累一次通行证,所以下面的代码如果将注释掉的部分都去掉,那么A线程就会被阻塞!
public static void main(String[] args) {
Thread a = new Thread(() -> {
System.out.println(Thread.currentThread().getName()+"休眠1s");
try {
TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {
e.printStackTrace();}
LockSupport.park();
//LockSupport.park();
System.out.println(Thread.currentThread().getName()+"解除阻塞成功!!!");
}, "A");
a.start();
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"发放通行证!!");
LockSupport.unpark(a);
//LockSupport.unpark(a);
},"B").start();
}
边栏推荐
- Separate the letters and numbers in the string so that the letters come first and the array comes last
- 面试题:MySQL的union all和union有什么区别、MySQL有哪几种join方式(阿里面试题)[通俗易懂]
- Electron学习(三)之简单交互操作
- pytest合集(2)— pytest运行方式
- Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
- MQ learning notes
- “丝路正青春 风采看福建”在闽外籍青年短视频大赛火热征集作品中
- 收到一封CTO来信,邀约面试机器学习工程师
- [intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
- PMP与NPDP之间的区别是什么?
猜你喜欢
Do you want to make up for the suspended examination in the first half of the year? Including ten examinations for supervision engineers, architects, etc
locust 系列入门
[deep learning] use deep learning to monitor your girlfriend's wechat chat?
Flume面试题
Go - exe corresponding to related dependency
焱融看 | 混合云时代下,如何制定多云策略
Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
小 P 周刊 Vol.11
十三届蓝桥杯B组国赛
收到一封CTO来信,邀约面试机器学习工程师
随机推荐
测试撤销1
十三届蓝桥杯B组国赛
News classification based on LSTM model
Pytest Collection (2) - mode de fonctionnement pytest
vscode的使用
Manually implement function isinstanceof (child, parent)
能升职加薪?PMP证书含金量浅析
MySQL learning notes - SQL optimization of optimization
首席信息官对高绩效IT团队定义的探讨和分析
php反射型xss,反射型XSS测试及修复
Which securities company should we choose to open an account for flush stock? Is it safe to open an account with a mobile phone?
浏览器tab页之间的通信
mysql 学习笔记-优化之SQL优化
Business visualization - make your flowchart'run'up
【深度学习】利用深度学习监控女朋友的微信聊天?
基于三维GIS的不动产管理应用
Getting started with the lockust series
Count the number of each character in the character
Talking from mlperf: how to lead the next wave of AI accelerator
从MLPerf谈起:如何引领AI加速器的下一波浪潮