当前位置:网站首页>详解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();
}

边栏推荐
- 从MLPerf谈起:如何引领AI加速器的下一波浪潮
- One of the basic learning of function
- 工控设备安全加密的意义和措施
- BlocProvider 为什么感觉和 Provider 很相似?
- 杰理之关于长按开机检测抬起问题【篇】
- 二叉树的基本操作
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- Introduction à l'ingénierie logicielle (sixième édition) notes d'examen de Zhang haifan
- 浏览器tab页之间的通信
- MQ学习笔记
猜你喜欢

K-means based user portrait clustering model

ngnix基础知识

Business visualization - make your flowchart'run'up

Significance and measures of security encryption of industrial control equipment

Pytest collection (2) - pytest operation mode

最近公共祖先离线做法(tarjan)

I received a letter from CTO inviting me to interview machine learning engineer
![[noip2013] building block competition [noip2018] road laying greed / difference](/img/d1/a56231cd4eb3cc1d91d8a55048ccfe.png)
[noip2013] building block competition [noip2018] road laying greed / difference

编程英语生词笔记本

Spark面试题
随机推荐
收到一封CTO来信,邀约面试机器学习工程师
MySQL empties table data
选择在同花顺上炒股开户可以吗?安全吗?
企业架构与项目管理的关联和区别
MySQL系列之事务日志Redo log学习笔记
Icml2022 | interventional contrastive learning based on meta semantic regularization
为什么数字化转型战略必须包括持续测试?
PCB线路板塞孔工艺的那些事儿~
What is the difference between consonants and Initials? (difference between initials and consonants)
杰理之关于长按开机检测抬起问题【篇】
[noip2013] building block competition [noip2018] road laying greed / difference
微信小程序,连续播放多段视频。合成一个视频的样子,自定义视频进度条
Mask wearing detection method based on yolov5
pytest合集(2)— pytest運行方式
Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖
linux下清理系统缓存并释放内存
物联网rfid等
Difference and use between require and import
Using closures to switch toggle by clicking a button