当前位置:网站首页>[JUC learning road day 8] condition
[JUC learning road day 8] condition
2022-07-01 22:55:00 【birdyson】
StampedLock
Implement the locking mechanism according to the timestamp , Used to solve the read-write lock when the number of read-write resource threads is unbalanced , The unfair situation of resource preemption . There are three lock processing modes supported in this class , Write lock respectively 、 Pessimistic locking 、 Optimism lock . Every complete StampedLock It is composed of version and mode , When acquiring the relevant lock, a digital tag stamp will be returned , Used to control the state of the lock , And the mark stamp is used to realize the unlocking processing .
Condition
When I first learned multithreading technology , Will use Object Class wait、notify Method to wait and wake up , Then in the traditional Thread Among classes , It also provides pause and resume functions ( This function has been abolished ),Condition It achieves the same function .
Condition It's an interface , Can rely on Lock Interface to implement .public Condition newCondition()
stay ReentranLock It can be found in the class that this method is composed of
sync.newCondition()Method called ;sync The object is AQS Implementation subclass , So the final
newcondition()The way is by AQS To complete ;public Condition newCondition() { return sync.newCondition(); }sync Implemented in the
new ConditionObject(), This is a AQS The inner class of , The implementation is as follows :public class ConditionObject implements Condition, java.io.Serializable { private static final long serialVersionUID = 1173984872572414699L; private transient ConditionNode firstWaiter; private transient ConditionNode lastWaiter; public ConditionObject() { }After analysis, we can find , stay Condition Internally, all waiting operation threads are actually saved in the queue , Because we can rely on AQS Provided CLH Mechanism to realize thread control .
Condition Realize thread waiting and wakeup :
package juc.condition2;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/** * @author birdy * @date 2022/6/25 10:58 AM */
public class Main {
public static String msg = null;
public static ReentrantLock reentrantLock = new ReentrantLock();
public static Condition condition = reentrantLock.newCondition();
public static void main(String[] args) {
try {
reentrantLock.lock();
new Thread(() -> {
try {
reentrantLock.lock();
TimeUnit.MILLISECONDS.sleep(2);
System.out.println("sub thread complete!");
msg = "hello!";
condition.signal();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
reentrantLock.unlock();
}
}).start();
condition.await();
} catch (Exception e) {
e.printStackTrace();
} finally {
reentrantLock.unlock();
System.out.println("unlocked");
}
System.out.println(msg);
}
}
LockSupport A little
边栏推荐
- Turn -- bring it and use it: share a gadget for checking memory leaks
- mixconv代码
- 友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…
- Hide the creation and use of users
- 今日睡眠质量记录71分
- Happy number [fast and slow pointer of ring PROBLEMS]
- 正则系列之量词(Quantifiers)
- 死锁的处理策略—预防死锁、避免死锁、检测和解除死锁
- Share some feelings of a programmer who has experienced layoffs twice a year
- Lc669. Prune binary search tree
猜你喜欢

【无标题】

"Trust machine" empowers development

使用 EMQX Cloud 实现物联网设备一机一密验证
![[MySQL] index classification](/img/73/fb9f28457b373ad35d2f6068387b47.png)
[MySQL] index classification

447-哔哩哔哩面经1

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 -- deduction of index storage model

思科考试--冗余网络
![[image segmentation] 2021 segformer neurips](/img/2f/a8631cbe9a46419b8dbd5205e1f5b5.png)
[image segmentation] 2021 segformer neurips

internal field separator
随机推荐
【扫盲】机器学习图像处理中的深层/浅层、局部/全局特征
Yolov5.5 call local camera
Flink SQL command line connection yarn
Configure filter
下班前几分钟,我弄清了v-model与.sync的区别
好友新书发布,祝贺(送福利)
友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…
3DE resources have nothing or nothing wrong
Use and function of spark analyze command map join broadcast join
[JUC learning road day 9] barrier derivatives
深度学习--数据操作
[QT widget] encapsulates a simple thread management class
思科考试--路由的概念和配置考试
Fiori 应用通过 Adaptation Project 的增强方式分享
ECMAScript 2022 正式发布,有你了解过的吗?
Pytorch nn.functional.unfold()的简单理解与用法
447-哔哩哔哩面经1
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条
今日睡眠质量记录71分
Understanding of indexes in MySQL