当前位置:网站首页>多线程打印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();
}
}
}
边栏推荐
猜你喜欢
【playwright】pytest-playwright增加代理服务选项
502 bad gateway原因、解决方法
el-table gets the data attribute of a row in the read data table
El - table column filter functions, control columns show and hide (effect and easy to implement full marks)
el-table获取读取数据表中某一行的数据属性
配置MSTP功能示例
【经验分享】配置用户通过Console口登录设备示例
MySQL性能优化(硬件,系统配置,表结构,SQL语句)
Docker安装Mysql
torch.nn.modules.activation.ReLU is not a Module subclass
随机推荐
重量级大咖来袭:阿里云生命科学与智能计算峰会精彩内容剧透
MySQL中的行锁
【项目案例】配置小型网络WLAN基本业务示例
信息学奥赛一本通T1453:移动玩具
MySQL忘记密码怎么办
docker-compose部署mysql
一家可靠的HDI板厂,需要具备哪些基本条件?
MySQL之concat的用法
UniApp 获取当前页面标题(navigationBarTitleText)
配置MSTP功能示例
spark中Repartition 和 Coalesce 区别
高密度 PCB 线路板设计中的过孔知识
El - tree to set focus on selected highlight highlighting, the selected node deepen background and change the font color, etc
Chrome 配置samesite=none方式
PCB板上的字母代表哪些元器件?一文看全!
MySQL的触发器
MySQL的on duplicate key update 的使用
Shell脚本--信号发送与捕捉
C语言实现通讯录功能(400行代码实现)
信息学奥赛一本通T1449:魔板