forked from lihengming/java-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYieldTest.java
More file actions
36 lines (31 loc) · 877 Bytes
/
Copy pathYieldTest.java
File metadata and controls
36 lines (31 loc) · 877 Bytes
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
package thread;
/**
* Created by on 2017/6/16.
*/
public class YieldTest {
public void work(){
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + " Working");
try{Thread.sleep(1000);}
catch(Exception e){
e.printStackTrace();
}
Thread.yield();
}
}
public static void main(String[] args) {
YieldTest test = new YieldTest();
new Thread(() -> test.work()).start();
new Thread(() -> test.work()).start();
System.out.println(Thread.currentThread().getName() + " Working");
/**
输出:
Thread-0 Working
Thread-1 Working
Thread-0 Working
Thread-1 Working
Thread-0 Working
Thread-1 Working
**/
}
}