码迷,mamicode.com
首页 > 编程语言 > 详细

线程,yield让出cpu调度

时间:2016-07-24 16:23:44      阅读:386      评论:0      收藏:0      [点我收藏+]

标签:

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+": @_@");
        }
    }

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            if(i%5==0){
                Thread.yield();
            }
            System.out.println(i+": yield");
        }
    }

}

结果:
(注,不是交替执行的。这里只是像而已)

0: @_@
0: yield
1: @_@
1: yield
2: @_@
2: yield
3: @_@
3: yield
4: @_@
4: yield
5: @_@
5: yield
6: @_@
6: yield
7: @_@
7: yield
8: @_@
8: yield
9: @_@
9: yield
10: @_@
10: yield
11: @_@
11: yield
12: @_@
12: yield
13: @_@
13: yield
14: @_@
14: yield
15: @_@
15: yield
16: @_@
16: yield
17: @_@
17: yield
18: @_@
18: yield
19: @_@
19: yield
20: @_@
20: yield
21: @_@
21: yield
22: @_@
22: yield
23: @_@
23: yield
24: @_@
24: yield
25: @_@
25: yield
26: @_@
26: yield
27: @_@
27: yield
28: @_@
28: yield
29: @_@
29: yield
30: @_@
30: yield
31: @_@
31: yield
32: @_@
32: yield
33: @_@
33: yield
34: @_@
34: yield
35: @_@
35: yield
36: @_@
36: yield
37: @_@
37: yield
38: @_@
38: yield
39: @_@
39: yield
40: @_@
40: yield
41: @_@
41: yield
42: @_@
42: yield
43: @_@
43: yield
44: @_@
44: yield
45: @_@
45: yield
46: @_@
46: yield
47: @_@
47: yield
48: @_@
48: yield
49: @_@
49: yield
50: @_@
50: yield
51: @_@
51: yield
52: @_@
52: yield
53: @_@
53: yield
54: @_@
54: yield
55: @_@
55: yield
56: @_@
56: yield
57: @_@
57: yield
58: @_@
58: yield
59: @_@
59: yield
60: @_@
60: yield
61: @_@
61: yield
62: @_@
62: yield
63: @_@
63: yield
64: @_@
64: yield
65: @_@
65: yield
66: @_@
66: yield
67: @_@
67: yield
68: @_@
68: yield
69: @_@
69: yield
70: @_@
70: yield
71: @_@
71: yield
72: @_@
72: yield
73: @_@
73: yield
74: @_@
74: yield
75: @_@
75: yield
76: @_@
76: yield
77: @_@
77: yield
78: @_@
78: yield
79: @_@
79: yield
80: @_@
80: yield

线程,yield让出cpu调度

标签:

原文地址:http://www.cnblogs.com/my-cup/p/5700820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!