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

边栏推荐
- 思科考试--路由的概念和配置考试
- Pytorch's code for visualizing feature maps after training its own network
- 思科--WAN 的概念考试外部工具
- 104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
- A few minutes before work, I found out V-model and The difference between sync
- leetcode - 287. Find duplicates
- "Trust machine" empowers development
- Origin2018 installation tutorial "recommended collection"
- LC501. Mode in binary search tree
- Fiori applications are shared through the enhancement of adaptation project
猜你喜欢

转--原来gdb的底层调试原理这么简单

Cut noodles C language

The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 release | geek headlines

Rust语言——小小白的入门学习05

internal field separator

C#/VB. Net to add text / image watermarks to PDF documents

Yolov5.5 call local camera

Intelligent computing architecture design of Internet

MySQL -- index of InnoDB storage engine

Fiori applications are shared through the enhancement of adaptation project
随机推荐
Little red book scheme jumps to the specified page
多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
下班前几分钟,我弄清了v-model与.sync的区别
leetcode - 287. Find duplicates
深度学习--数据操作
[untitled]
104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
Awoo's favorite problem (priority queue)
Digital currency: far-reaching innovation
Two schemes of transforming the heat map of human posture estimation into coordinate points
3DE resources have nothing or nothing wrong
【无标题】
Turn -- the underlying debugging principle of GDB is so simple
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
Using emqx cloud to realize one machine one secret verification of IOT devices
MySQL中对于索引的理解
好友新书发布,祝贺(送福利)
Chen Tianqi's machine learning compilation course (free)
Intelligent computing architecture design of Internet
[image segmentation] 2021 segformer neurips