-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadYield.java
More file actions
46 lines (41 loc) · 1.18 KB
/
ThreadYield.java
File metadata and controls
46 lines (41 loc) · 1.18 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
package org.cp;
/**
* create by CP on 2019/7/23 0023.
*/
public class ThreadYield {
private static Integer i = 10;
public static void main(String[] args) {
Thread t2 = new Thread(() -> {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("T2...");
}
});
Thread t1 = new Thread(() -> {
do {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ": " + i);
i--;
if (i < 5) {
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(t2.isAlive());
} while (i >= 0);
});
t1.setName("T1 ");
t1.start();
// t2.start();
}
}