当前位置:网站首页>线程间定制化通信(ReentrantLock)
线程间定制化通信(ReentrantLock)
2022-04-22 05:33:00 【linsa_pursuer】
启动三个线程
*AA打印5次,BB打印10次,CC打印15次 进行10轮
package juc.two;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//第一步 创建资源类
class ShareResource {
//定义标志位
private int flag = 1; // 1 AA 2 BB 3 CC
//创建Lock锁
private Lock lock = new ReentrantLock();
//创建三个condition
private Condition c1 = lock.newCondition();
private Condition c2 = lock.newCondition();
private Condition c3 = lock.newCondition();
//打印5次,参数第几轮
public void print5(int loop) throws InterruptedException {
//上锁
lock.lock();
try {
//判断
while (flag != 1){
//等待
c1.await();
}
//干活
for (int i = 1; i <=5;i++){
System.out.println(Thread.currentThread().getName()+" :: "+i+" :轮数:"+loop);
}
//通知
flag = 2;//修改标志位 2
c2.signal();//通知BB线程
}finally {
//释放锁
lock.unlock();
}
}
//打印10参数第几轮
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+" :轮数:"+loop);
}
//修改标志位
flag = 3;
//通知CC线程
c3.signal();
}finally {
lock.unlock();
}
}
//打印15参数第几轮
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+" :轮数:"+loop);
}
//修改标志位
flag = 1;
//通知AA线程
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://blog.csdn.net/linsa_pursuer/article/details/124310563
边栏推荐
- 2022.4.21-----leetcode. eight hundred and twenty-four
- MySQL 第7章 对数据表的复杂查询
- C language version: static establishment of binary tree
- Fundamentals of graphics - flood
- mysql表删除重复值,只保留一个
- Integer source code
- Dolphin DB vscode plug-in tutorial
- 剑指 Offer 22. 链表中倒数第k个节点
- 再见2020,2021我来了
- Auto.js 画布设置防锯齿paint.setAntiAlias(true);
猜你喜欢
随机推荐
有限单元法基本原理和数值方法_有限元法基本原理
MySQL Chapter 7 complex query of data table
2022.4.21-----leetcode.824
I/O基础知识入门
Online Tetris with automatic hang-up source code
MySQL JDBC 编程
Unreal engine sequence effect and audio binding trigger
Unity built-in terrain optimization
枚舉和Lambda錶達式
【无标题】GBase 8s V8.8 SQL 指南:教程-6
MySQL 第7章 对数据表的复杂查询
mysql非常用命令
Do you know the three implementation methods of strlen?
idea2021. 1. When writing SQL in mapper: unable to resolve column / table
基于有限体积法的传热拓扑优化
2022.4.21-----leetcode. eight hundred and twenty-four
Enumerations and lambda expressions
unity接入ILRuntime之后 热更工程应用Packages下面的包方法 例如Unity.RenderPipelines.Core.Runtime包
Unity first order Bezier curve for throwing objects
随机字符串工具类RandomStringUtils详解









