当前位置:网站首页>Multi thread alternate output ab
Multi thread alternate output ab
2022-06-11 03:21:00 【Rookie nest】
Mode one
public class T1 {
public static void main(String[] args) {
new T1().fun();
}
private void fun() {
try {
Object lock = new Object();
Thread thread1 = new Thread(() -> {
try {
synchronized (lock) {
for (int i = 0; i < 10; i++) {
System.out.println("A");
lock.notify();
lock.wait();
}
}
} catch (Exception e) {
e.printStackTrace();
}
});
Thread thread2 = new Thread(() -> {
try {
synchronized (lock) {
for (int i = 0; i < 10; i++) {
lock.wait();
System.out.println("B");
lock.notify();
}
}
} catch (Exception e) {
e.printStackTrace();
}
});
thread2.start();
Thread.sleep(100);
thread1.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Mode two
package look.LookUse;
public class T2 {
static Thread ta, tb;
public static void main(String[] args) {
int num = 10;
Thread lock = Thread.currentThread();
ta = new Thread(() -> {
for (int i = 0; i < num; i++) {
try {
while (tb.getState() != Thread.State.WAITING) {
}
synchronized (lock) {
System.out.println("A" + i);
lock.notify();
if (i != num - 1)
lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
tb = new Thread(() -> {
for (int i = 0; i < num; i++) {
try {
synchronized (lock) {
lock.notify();
lock.wait();
System.out.println("B" + i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
ta.start();
tb.start();
}
}
边栏推荐
- 名不副实的雅迪高端品牌VFLY,为何“不高端”?
- TimeHelper
- [safety science popularization] mining technology starts from the love story of a man of science and Engineering
- WinDbg virtual machine dual machine debugging driver file debugging
- B_QuRT_User_Guide(19)
- Go quick start of go language (I): the first go program
- cv. Matchtemplate image model matching opencv
- postgresql 函数的参数为自定义类型时传参格式
- C语言数组与指针练习题
- Dépannage du problème de retard des données de communication du micro - ordinateur à puce unique
猜你喜欢

If the source code of the home page module is not separated ----- > nanny level source code analysis (1)

删除CSDN上传图片的水印

2022年6月中国数据库排行榜:TiDB卷土重来摘桂冠,达梦蛰伏五月夺探花

Go language advantages and learning Roadmap

Mazhiqiang: research progress and application of speech recognition technology -- RTC dev Meetup

pip 安装 qt5 。

MySQL learning notes: JSON nested array query

Log4j:error category option "1" not a decimal integer

ASLR
![[safety science popularization] mining technology starts from the love story of a man of science and Engineering](/img/01/73376c133c33527e479685f8680238.jpg)
[safety science popularization] mining technology starts from the love story of a man of science and Engineering
随机推荐
PostgreSQL source code learning (22) - fault recovery ③ - transaction log registration
Logical deletion_ Swagger2 framework integration
[safety science popularization] have you been accepted by social workers today?
Disk quota exceeded
马志强:语音识别技术研究进展和应用落地分享丨RTC Dev Meetup
has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested
Integrated MP code generator
svg实现纸飞机自由的飞翔动画
單片機通信數據延遲問題排查
Shangpinhui mall_ Background homepage of
Basic use of sonarqube platform
Fuluo classic source code Fuluo classic system development principle sharing
postgresql源码学习(22)—— 故障恢复③-事务日志的注册
Harris corner detection opencv
postgresql 函数的参数为自定义类型时传参格式
postgresql源码学习(十七)—— MVCC②-快照与隔离级别简介
canvas+svg线条粒子动画网页背景
Difference between idea open and import project
How to ensure reliable delivery of messages?
postgresql 捕获函数中的异常