public class Yield01 extends Thread { public static void main(String[] args) { new Yield01().start(); for (int i = 0; i < 1000; i++) { if(i%5==0){ Thread.yield(); } System.out.println(i+": @[email protected]"); } } @Override public void run() { for (int i = 0; i < 1000; i++) { if(i%5==0){ Thread.yield(); } System.out.println(i+": yield"); } } }
结果:
(注,不是交替执行的。这里只是像而已)
0: @[email protected]
0: yield
1: @[email protected]
1: yield
2: @[email protected]
2: yield
3: @[email protected]
3: yield
4: @[email protected]
4: yield
5: @[email protected]
5: yield
6: @[email protected]
6: yield
7: @[email protected]
7: yield
8: @[email protected]
8: yield
9: @[email protected]
9: yield
10: @[email protected]
10: yield
11: @[email protected]
11: yield
12: @[email protected]
12: yield
13: @[email protected]
13: yield
14: @[email protected]
14: yield
15: @[email protected]
15: yield
16: @[email protected]
16: yield
17: @[email protected]
17: yield
18: @[email protected]
18: yield
19: @[email protected]
19: yield
20: @[email protected]
20: yield
21: @[email protected]
21: yield
22: @[email protected]
22: yield
23: @[email protected]
23: yield
24: @[email protected]
24: yield
25: @[email protected]
25: yield
26: @[email protected]
26: yield
27: @[email protected]
27: yield
28: @[email protected]
28: yield
29: @[email protected]
29: yield
30: @[email protected]
30: yield
31: @[email protected]
31: yield
32: @[email protected]
32: yield
33: @[email protected]
33: yield
34: @[email protected]
34: yield
35: @[email protected]
35: yield
36: @[email protected]
36: yield
37: @[email protected]
37: yield
38: @[email protected]
38: yield
39: @[email protected]
39: yield
40: @[email protected]
40: yield
41: @[email protected]
41: yield
42: @[email protected]
42: yield
43: @[email protected]
43: yield
44: @[email protected]
44: yield
45: @[email protected]
45: yield
46: @[email protected]
46: yield
47: @[email protected]
47: yield
48: @[email protected]
48: yield
49: @[email protected]
49: yield
50: @[email protected]
50: yield
51: @[email protected]
51: yield
52: @[email protected]
52: yield
53: @[email protected]
53: yield
54: @[email protected]
54: yield
55: @[email protected]
55: yield
56: @[email protected]
56: yield
57: @[email protected]
57: yield
58: @[email protected]
58: yield
59: @[email protected]
59: yield
60: @[email protected]
60: yield
61: @[email protected]
61: yield
62: @[email protected]
62: yield
63: @[email protected]
63: yield
64: @[email protected]
64: yield
65: @[email protected]
65: yield
66: @[email protected]
66: yield
67: @[email protected]
67: yield
68: @[email protected]
68: yield
69: @[email protected]
69: yield
70: @[email protected]
70: yield
71: @[email protected]
71: yield
72: @[email protected]
72: yield
73: @[email protected]
73: yield
74: @[email protected]
74: yield
75: @[email protected]
75: yield
76: @[email protected]
76: yield
77: @[email protected]
77: yield
78: @[email protected]
78: yield
79: @[email protected]
79: yield
80: @[email protected]
80: yield