当前位置:网站首页>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();
}
边栏推荐
- Electron学习(三)之简单交互操作
- MySQL中对于事务的理解
- SAP GUI 里的收藏夹事务码管理工具
- 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
- 分享一个一年经历两次裁员的程序员的一些感触
- [MySQL] index classification
- General use of qstringlist
- Flink SQL command line connection yarn
- 正则系列之量词(Quantifiers)
- [literacy] deep / shallow, local / global features in machine learning image processing
猜你喜欢
轉載csdn文章操作
tcpdump命令使用详解
Ffmpeg learning notes
Single step debugging analysis of rxjs observable of operator
447-哔哩哔哩面经1
Today's sleep quality record 71 points
元宇宙可能成为互联网发展的新方向
[jetcache] how to use jetcache
Stimulate new kinetic energy and promote digital economy in multiple places
vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
随机推荐
"Trust machine" empowers development
Selection of all-optical technology in the park - Part 2
[daily training] 326 Power of 3
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
ESP自动下载电路设计
Today's sleep quality record 71 points
Origin2018安装教程「建议收藏」
el-input文本域字数限制,超过显示变红并禁止输入
[daily training] 66 add one-tenth
转--拿来即用:分享一个检查内存泄漏的小工具
Explain kubernetes network model in detail
ECMAScript 2022 正式发布,有你了解过的吗?
Wechat open platform scanning code login [easy to understand]
数字货币:影响深远的创新
转载csdn文章操作
激发新动能 多地发力数字经济
Stimulate new kinetic energy and promote digital economy in multiple places
【扫盲】机器学习图像处理中的深层/浅层、局部/全局特征
Understanding of transactions in MySQL
切面条 C语言