Skip to content

Commit d4f3666

Browse files
author
bl03615
committed
5.(选做)跑一跑课上的各个例子,加深对多线程的理解
1 parent 1819261 commit d4f3666

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package thread.github.pxd;
2+
3+
public class ThreadABCPrint {
4+
5+
volatile static int completeThread = 0;
6+
7+
public static void main(String[] args) {
8+
9+
Thread thread1 = new Thread(() -> {
10+
while (true){
11+
12+
if (completeThread == 3 || completeThread == 0) {
13+
System.out.print("A");
14+
completeThread = 1;
15+
}
16+
}
17+
});
18+
19+
Thread thread2 = new Thread(() -> {
20+
while (true){
21+
22+
if (completeThread == 1) {
23+
System.out.print("B");
24+
completeThread = 2;
25+
}
26+
}
27+
});
28+
29+
Thread thread3 = new Thread(() -> {
30+
while (true){
31+
32+
if (completeThread == 2) {
33+
System.out.print("C");
34+
completeThread = 3;
35+
}
36+
}
37+
});
38+
39+
thread1.start();
40+
thread2.start();
41+
thread3.start();
42+
}
43+
44+
}

0 commit comments

Comments
 (0)