-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToys.java
More file actions
54 lines (48 loc) · 1.43 KB
/
Copy pathToys.java
File metadata and controls
54 lines (48 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package org.cp;
/**
* create by CP on 2019/7/24 0024.
*/
public class Toys {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
int i = 1;
@Override
public void run() {
while (i<=3) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("吃 "+i+" 个馒头");
i++;
}
}
};
Thread t2 = new Thread(runnable);
Thread t1 = new Thread(){
int toyCount = 1;
@Override
public void run() {
while (toyCount<=50) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("TOY Number: "+toyCount);
toyCount++;
if (toyCount==21) {
try {
t2.start();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
t1.start();
}
}