当前位置:网站首页>多线程交替输出AB
多线程交替输出AB
2022-06-11 03:13:00 【菜鸟小窝】
方式一
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();
}
}
}
方式二
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();
}
}
边栏推荐
- Troubleshooting of single chip microcomputer communication data delay
- The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
- cv. Houghcircles: Circular Hough transform opencv
- Array full permutation
- MySQL learning notes: JSON nested array query
- Helm deploy traifik ingress
- com. mchange. v2.c3p0. Combopooleddatasource red
- Solr import MySQL database report: Data config problem: invalid byte 2 of 2-byte UTF-8 sequence
- 亚马逊测评自养号,小白应该如何开始?
- Discussion on the concurrency security of a common set on players
猜你喜欢

com. mchange. v2.c3p0. Combopooleddatasource red

B_QuRT_User_Guide(18)

Error in Solr access: error initializing queryelevationcomponent

PIP installation Qt5.

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

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

Delete the watermark of the picture uploaded by CSDN

Pyqt5:qlineedit control code

Operations on annotation and reflection

Dépannage du problème de retard des données de communication du micro - ordinateur à puce unique
随机推荐
postgresql源码学习(21)—— 故障恢复②-事务日志初始化
com. mchange. v2.c3p0. Combopooleddatasource red
音乐正版率关键数据缺失,网易云音乐IPO胜算几何?
【安全科普】今天你被社工了吗?
Gd32 can sends no mailbox fault
Fuluo classic source code Fuluo classic system development principle sharing
sonarqube平台基础使用
单片机通信数据延迟问题排查
Visit the swagger times unable to infer base url
Pyqt5: basic usage of label control
Artalk | how to build a domestic hyperfusion evolutionary base with minimum investment?
Disk quota exceeded
Troubleshooting of single chip microcomputer communication data delay
B / Qurt Utilisateur Guide (19)
Android WiFi hide SSID
LVGL中文字体制作
Minimum common ancestor of binary tree
cv. Houghcircles: Circular Hough transform opencv
Pyqt5:slider slider control
023 MySQL index optimization tips - common cases of index failure