Lock实现线程间定制化通信
合集 - JUC基础(13)
1.
JUC前置知识
2023-10-23
2.
Synchronized和Lock接口
2023-10-25
3.
线程间通信
2023-10-28
4.
Lock实现线程间定制化通信
2023-10-30
5.
常用集合线程安全分析
2023-11-02
6.
多线程锁
2023-11-12
7.
Callable接口和Future接口
2023-11-20
8.
JUC的强大辅助类
2023-11-22
9.
ReentrantReadWriteLock读写锁
2023-11-24
10.
BlockingQueue阻塞队列
2023-11-26
11.
线程池
2023-11-28
12.
Fork/Join
2023-12-01
13.
CompletableFuture异步回调
2023-12-01
收起
Lock实现线程间定制化通信
案例
要求
三个线程,AA BB CC AA线程打印5次,BB线程打印10次,CC线程打印15次
代码实现
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
-
@author 长名06
-
@version 1.0
-
线程定制化通信
*/
//第一步,创建共享资源,和操作方法
class ShareFlag {private Lock lock = new ReentrantLock();
private int flag = 1;private Condition c1 = lock.newCondition();//一个Condition对象,只能唤醒由该对象阻塞的线程
private Condition c2 = lock.newCondition();
private Condition c3 = lock.newCondition();public void print5(int loop) throws InterruptedException {
lock.lock();try { //第四步,避免虚假唤醒现象 while (flag != 1) {//第2.1步 判断是否满足线程工作条件,阻塞 c1.await(); } //第2.2步 具体执行 for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + "\t" + i +"次输出" + "flag=" + flag + "轮数" + loop); } flag = 2; //通知(唤醒等待线程) c2.signal(); } finally { lock.unlock(); }
}
public void print10(int loop) throws InterruptedException {
lock.lock();try { while (flag != 2) { c2.await(); } for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + "\t" + i +"次输出" + "flag=" + flag + "轮数" + loop); } flag = 3; //通知(唤醒等待线程) c3.signal(); } finally { lock.unlock(); }
}
public void print15(int loop) throws InterruptedException {
lock.lock();try { while (flag != 3) { c3.await(); } for (int i = 0; i < 15; i++) { System.out.println(Thread.currentThread().getName() + "\t" + i +"次输出" + "flag=" + flag + "轮数" + loop); } flag = 1; //通知(唤醒等待线程)
//代码效果参考:https://www.weibow.com/sitemap/post.xml
c1.signal();
} finally {
lock.unlock();
}
}
}
public class ThreadDemoByCust {
public static void main(String[] args) {
ShareFlag shareFlag = new ShareFlag();
new Thread(() -> {
for (int i = 1; i <= 10; i++) {
try {
shareFlag.print5(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "AA").start();
new Thread(() -> {
for (int i = 1; i <= 10; i++) {
try {
shareFlag.print10(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "BB").start();
new Thread(() -> {
for (int i = 1; i <= 10; i++) {
try {
shareFlag.print15(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "CC").start();
}
}
学习时,关于代码的疑问
不理解,为什么一定是,AA线程执行完后,,BB线程执行完后,是c3是c2去执行唤醒一个等待线程操作去执行唤醒一个等待线程的操作,CC线程执行完后,是c1执行唤醒一个等待线程的操作。先往后面看,后续回来解答这个问题。
解答
是为了满足要求中的顺序,最开始三个AA,BB,CC线程,并行执行,因为flag初始值是1,所以AA先执行第一次循环,执行print5,然后循环输出5次后,flag = 2了(AA被c1阻塞),为了保证要求,再BB输出10次,则需要使用c2去唤醒BB线程,为什么是c2,因为BB线程在,最开始flag = 1时,是由c2去阻塞的,这里如果是c3对象阻塞的,则必须要用c3对象唤醒,因为Condition对象,只能唤醒由该对象阻塞的线程。后面BB线程切换CC线程,CC线程切换AA线程也是同理。
只是为了记录自己的学习历程,且本人水平有限,不对之处,请指正。
标签:AA,BB,CC,lock,flag,线程,2023 From: https://www.cnblogs.com/longshao2024/p/18390400