当前位置:网站首页>Customized communication between threads (reentrantlock)
Customized communication between threads (reentrantlock)
2022-04-23 06:09:00 【linsa_ pursuer】
Start three threads
*AA Print 5 Time ,BB Print 10 Time ,CC Print 15 Time Conduct 10 round
package juc.two;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
// First step Create a resource class
class ShareResource {
// Define flag bit
private int flag = 1; // 1 AA 2 BB 3 CC
// establish Lock lock
private Lock lock = new ReentrantLock();
// Create three condition
private Condition c1 = lock.newCondition();
private Condition c2 = lock.newCondition();
private Condition c3 = lock.newCondition();
// Print 5 Time , Parameter round number
public void print5(int loop) throws InterruptedException {
// locked
lock.lock();
try {
// Judge
while (flag != 1){
// wait for
c1.await();
}
// work
for (int i = 1; i <=5;i++){
System.out.println(Thread.currentThread().getName()+" :: "+i+" : Number of rounds :"+loop);
}
// notice
flag = 2;// Modify flag bit 2
c2.signal();// notice BB Threads
}finally {
// Release the lock
lock.unlock();
}
}
// Print 10 Parameter round number
public void print10(int loop) throws InterruptedException {
lock.lock();
try {
while (flag != 2){
c2.await();
}
for (int i = 1; i <=10;i++){
System.out.println(Thread.currentThread().getName()+" :: "+i+" : Number of rounds :"+loop);
}
// Modify flag bit
flag = 3;
// notice CC Threads
c3.signal();
}finally {
lock.unlock();
}
}
// Print 15 Parameter round number
public void print15(int loop) throws InterruptedException {
lock.lock();
try {
while (flag != 3){
c3.await();
}
for (int i = 1; i <=15;i++){
System.out.println(Thread.currentThread().getName()+" :: "+i+" : Number of rounds :"+loop);
}
// Modify flag bit
flag = 1;
// notice AA Threads
c1.signal();
}finally {
lock.unlock();
}
}
}
public class ThreadDemo3 {
public static void main(String[] args) {
ShareResource shareResource = new ShareResource();
new Thread(()->{
for(int i = 1;i <=10; i++){
try {
shareResource.print5(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"AA").start();
new Thread(()->{
for(int i = 1;i <=10; i++){
try {
shareResource.print10(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"BB").start();
new Thread(()->{
for(int i = 1;i <=10; i++){
try {
shareResource.print15(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"CC").start();
}
}
版权声明
本文为[linsa_ pursuer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220533259365.html
边栏推荐
- Numpy common function table sorting of data processing
- Pytorch学习记录(四):参数初始化
- Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
- Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
- Pyqy5 learning (III): qlineedit + qtextedit
- Why does the subscript of the array start from 0 instead of 1?
- Linear algebra Chapter 2 - matrices and their operations
- Multithreading and high concurrency (1) -- basic knowledge of threads (implementation, common methods, state)
- Pytoch -- data loading and processing
- 图像恢复论文简记——Uformer: A General U-Shaped Transformer for Image Restoration
猜你喜欢
Preparedstatement prevents SQL injection
Delete and truncate
编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
CONDA virtual environment management (create, delete, clone, rename, export and import)
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索
Solve the error: importerror: iprogress not found Please update jupyter and ipywidgets
IO multiplexing of 09 redis
Anaconda
随机推荐
Graphic numpy array matrix
Create enterprise mailbox account command
Traitement des séquelles du flux de Tensor - exemple simple d'enregistrement de torche. Utils. Données. Dataset. Problème de dimension de l'image lors de la réécriture de l'ensemble de données
The user name and password of users in the domain accessing the samba server outside the domain are wrong
DBCP usage
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
Solve the error: importerror: iprogress not found Please update jupyter and ipywidgets
Shansi Valley P290 polymorphism exercise
Algèbre linéaire chapitre 1 - déterminants
Problems and solutions of database migration
Kingdee EAS "general ledger" system calls "de posting" button
PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包
Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
Best practices for MySQL storage time
卡尔曼滤波与惯性组合导航
What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
Pytorch learning record (7): skills in processing data and training models
Fact final variable and final variable
Stability building best practices
Get the value of state in effects in DVA