当前位置:网站首页>[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
边栏推荐
猜你喜欢
激发新动能 多地发力数字经济
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
使用 Three.js 实现'雪糕'地球,让地球也凉爽一夏
SAP GUI 里的收藏夹事务码管理工具
ESP自动下载电路设计
Congratulations on the release of friends' new book (send welfare)
Favorite transaction code management tool in SAP GUI
Sogou wechat app reverse (II) so layer
陈天奇的机器学习编译课(免费)
C#/VB. Net to add text / image watermarks to PDF documents
随机推荐
Reprint CSDN article operation
下班前几分钟,我弄清了v-model与.sync的区别
今日睡眠质量记录71分
转载csdn文章操作
leetcode - 287. Find duplicates
The fixed assets management subsystem reports are divided into what categories and which accounts are included
mixconv代码
"Trust machine" empowers development
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
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
Origin2018安装教程「建议收藏」
轉載csdn文章操作
Use and function of spark analyze command map join broadcast join
Congratulations on the release of friends' new book (send welfare)
Hide the creation and use of users
Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle
Measurement of reference loop gain and phase margin
Genicam gentl standard ver1.5 (4) Chapter 5 acquisition engine
rxjs Observable of 操作符的单步调试分析
Ffmpeg learning notes