当前位置:网站首页>多线程打印ABC(继承+进阶)
多线程打印ABC(继承+进阶)
2022-08-03 05:41:00 【是小鱼儿哈】
基础

进阶

public class Demo {
private static Object locker1 = new Object();
private static Object locker2 = new Object();
private static Object locker3 = new Object();
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
synchronized (locker1) {
locker1.wait();
}
System.out.print("A");
synchronized (locker2) {
locker2.notify();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread t2 = new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
synchronized (locker2) {
locker2.wait();
}
System.out.print("B");
synchronized (locker3) {
locker3.notify();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread t3 = new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
synchronized (locker3) {
locker3.wait();
}
System.out.println("C");
synchronized (locker1) {
locker1.notify();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t1.start();
t2.start();
t3.start();
Thread.sleep(1000);
// 从线程 t1 启动
synchronized (locker1) {
locker1.notify();
}
}
}边栏推荐
猜你喜欢
随机推荐
502 bad gateway原因、解决方法
2021年PHP-Laravel面试题问卷题 答案记录
el-tree设置利用setCheckedNodessetCheckedKeys默认勾选节点,以及通过setChecked新增勾选指定节点
Charles capture shows
solution Chrome configuration samesite=none method
MySQL中,对结果或条件进行字符串拼接
VO、DTO、DO、POJO的区别和概念
零代码工具拖拽流程图
IEEE RAL投初稿
阿里云-武林头条-建站小能手争霸赛
IPV4地址详解
docker-compose部署mysql
DIFM network, rounding and repetition
MySQL的DATE_FORMAT()函数将Date转为字符串
pyspark --- count the mode of multiple columns and return it at once
C语言实现通讯录功能(400行代码实现)
Laravel 中使用子查询
pyspark --- 统计多列的众数并一次返回
UniApp scroll-view 事件不生效(@scroll、@scrolltolower、@scrolltoupper ...)
El - tree set using setCheckedNodessetCheckedKeys default check nodes, and a new check through setChecked specified node








