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

边栏推荐
- Cloud Vulnerability Global Database
- [target tracking] | single target tracking indicator
- Detailed explanation of common configurations in redis configuration file [easy to understand]
- 思科考试--冗余网络
- Today's sleep quality record 71 points
- pytorch训练自己网络后可视化特征图谱的代码
- Flink SQL command line connection yarn
- Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle
- General use of qstringlist
- “信任机器”为发展赋能
猜你喜欢

Flynk SQL client uses comparison and is familiar with official documents

2020-ViT ICLR

数字货币:影响深远的创新

Share some feelings of a programmer who has experienced layoffs twice a year

Yolov5.5 call local camera

rxjs Observable of 操作符的单步调试分析

Cutefishos system~

分享一个一年经历两次裁员的程序员的一些感触

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

Map container
随机推荐
转--拿来即用:分享一个检查内存泄漏的小工具
Using emqx cloud to realize one machine one secret verification of IOT devices
Chen Tianqi's machine learning compilation course (free)
Understanding of indexes in MySQL
小红书Scheme跳转到指定页面
leetcode - 287. Find duplicates
MySQL中对于事务的理解
Arlo's thinking after confusion
Resttemplate remote call tool class
Using securecrtportable to remotely connect virtual machines
第三方验收测试有什么好处?专业第三方软件测试机构推荐
轉載csdn文章操作
[QT widget] encapsulates a simple thread management class
QStringList 的常规使用
Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
Origin2018安装教程「建议收藏」
Appium自动化测试基础 — APPium安装(一)
转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程
447-哔哩哔哩面经1
Awoo's favorite problem (priority queue)