当前位置:网站首页>Explain the use of locksupport in detail
Explain the use of locksupport in detail
2022-07-01 22:55:00 【Keyboard singer】
One 、 brief introduction
1. The picture below is Java api Description in :
2. Its methods are as follows , The circled method is the core method :
3. Common methods of waking up and blocking threads include LockSupport:
Two 、LockSupport Advantages of wake-up blocking method
1.Object Class provides the wait
and notify
Need to be in synchronized Used in code blocks , Otherwise it will be reported IllegalMonitorStateException
abnormal ! also notify
Must be in wait Method can take effect only after it is executed .( Here is the correct usage !)
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 Awakened ");
}
},"A").start();
// Sleep 1 second
try {
TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {
e.printStackTrace();}
new Thread(()->{
synchronized (objectlock) {
objectlock.notify();
System.out.println(Thread.currentThread().getName()+": \t A notice !");
}
},"B").start();
}
2.ReentrantLock Class provides the await
and signal
There is still the problem of execution sequence , The following case makes signal Method first , After the results are found await It didn't wake up !
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
new Thread(()->{
// Pause 1 second , Give Way B The line program is executed first !
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 Awakened ");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
},"A").start();
new Thread(()->{
lock.lock();
try {
condition.signal();
System.out.println(Thread.currentThread().getName()+": \t A notice !");
}finally {
lock.unlock();
}
},"B").start();
}
3.LockSupport There is no such problem , Similar to highway ETC, Bought the pass in advance unpark
, Directly lift the railing at the gate and let it go park
Intercepted . but unpark Repeated execution can only accumulate one pass , So if the commented parts of the following code are removed , that A The thread will be blocked !
public static void main(String[] args) {
Thread a = new Thread(() -> {
System.out.println(Thread.currentThread().getName()+" Sleep 1s");
try {
TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {
e.printStackTrace();}
LockSupport.park();
//LockSupport.park();
System.out.println(Thread.currentThread().getName()+" Unblock successful !!!");
}, "A");
a.start();
new Thread(()->{
System.out.println(Thread.currentThread().getName()+" Issue passes !!");
LockSupport.unpark(a);
//LockSupport.unpark(a);
},"B").start();
}
边栏推荐
- 转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程
- Friendly serial assistant tutorial_ How to configure friendly serial port debugging assistant - tutorial on using friendly serial port debugging assistant
- 3DE resources have nothing or nothing wrong
- Detailed explanation of common configurations in redis configuration file [easy to understand]
- 2020-ViT ICLR
- Rust语言——小小白的入门学习05
- Turn -- the underlying debugging principle of GDB is so simple
- 陈天奇的机器学习编译课(免费)
- 【扫盲】机器学习图像处理中的深层/浅层、局部/全局特征
- Resttemplate remote call tool class
猜你喜欢
tcpdump命令使用详解
Appium automation test foundation - appium installation (I)
多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
死锁的处理策略—预防死锁、避免死锁、检测和解除死锁
Congratulations on the release of friends' new book (send welfare)
Using emqx cloud to realize one machine one secret verification of IOT devices
[jetcache] how to use jetcache
Demo program implementation of QT version Huarui camera
el-input文本域字数限制,超过显示变红并禁止输入
Mysql5.7 set password policy (etc. three-level password transformation)
随机推荐
Selection of all-optical technology in the park - Part 2
Understanding of indexes in MySQL
What class loading mechanisms does the JVM have?
【无标题】
Little red book scheme jumps to the specified page
Fiori 应用通过 Adaptation Project 的增强方式分享
深度学习--数据操作
MySQL -- index of MyISAM storage engine
Detailed explanation of common configurations in redis configuration file [easy to understand]
【扫盲】机器学习图像处理中的深层/浅层、局部/全局特征
Awoo's favorite problem (priority queue)
[image segmentation] 2021 segformer neurips
MySQL中对于索引的理解
cvpr2022 human pose estiamtion
使用 EMQX Cloud 实现物联网设备一机一密验证
Arlo's thinking after confusion
Appium自动化测试基础 — APPium安装(一)
SAP GUI 里的收藏夹事务码管理工具
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
思科考试--路由的概念和配置考试