当前位置:网站首页>[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
边栏推荐
- 下班前几分钟,我弄清了v-model与.sync的区别
- Electron学习(三)之简单交互操作
- [literacy] deep / shallow, local / global features in machine learning image processing
- Two schemes of transforming the heat map of human posture estimation into coordinate points
- MySQL stored procedure
- Demo program implementation of QT version Huarui camera
- [jetcache] how to use jetcache
- Lc669. Prune binary search tree
- Configure filter
- Congratulations on the release of friends' new book (send welfare)
猜你喜欢

转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程

Quantifiers of regular series

使用 EMQX Cloud 实现物联网设备一机一密验证

Metauniverse may become a new direction of Internet development

Preparation of functional test report

Mysql5.7 set password policy (etc. three-level password transformation)

使用 Three.js 实现'雪糕'地球,让地球也凉爽一夏

map容器

Daily question brushing record (10)

MySQL中对于事务的理解
随机推荐
Pytorch's code for visualizing feature maps after training its own network
Turn -- the underlying debugging principle of GDB is so simple
104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
nn.Parameter】Pytorch特征融合自适应权重设置(可学习权重使用)
思科--高可用和高可靠网络考试
【c语言】malloc函数详解[通俗易懂]
效率提升 - 鼓捣个性化容器开发环境
el-input文本域字数限制,超过显示变红并禁止输入
Use and function of spark analyze command map join broadcast join
Appium automated testing foundation - Supplement: introduction to desired capabilities parameters
[MySQL] index classification
SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time
MySQL5.7 设置密码策略(等保三级密码改造)
3DE resources have nothing or nothing wrong
转--拿来即用:分享一个检查内存泄漏的小工具
104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
QStringList 的常规使用
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
twenty million two hundred and twenty thousand seven hundred and one
正则系列之组和范围(Groups and Ranges)