A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© die_er 初级黑马   /  2014-3-23 17:37  /  1577 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public void run() {
               
                for(int i=0;i<60; i++)
                {
                        System.out.println(Thread.currentThread().getName()+"thread run"+i);
                        Thread.yield();
                }
        }
两个线程,不是应该间隔输出吗?为什么我的结果都是
Thread-0thread run29
Thread-0thread run30
Thread-0thread run31
Thread-0thread run32
Thread-0thread run33
Thread-0thread run34
Thread-0thread run35
Thread-0thread run36
Thread-0thread run37
Thread-0thread run38
Thread-0thread run39
Thread-0thread run40
Thread-0thread run41
Thread-0thread run42
Thread-1thread run0
Thread-1thread run1
Thread-1thread run2
Thread-1thread run3
Thread-1thread run4
Thread-1thread run5
Thread-1thread run6
Thread-1thread run7
Thread-1thread run8
Thread-1thread run9
Thread-1thread run10
Thread-1thread run11
Thread-1thread run12
Thread-1thread run13
Thread-1thread run14
Thread-1thread run15
Thread-0thread run43
Thread-1thread run16
Thread-0thread run44
Thread-0thread run45
Thread-1thread run17
Thread-0thread run46
Thread-0thread run47
Thread-0thread run48
Thread-0thread run49
Thread-0thread run50
Thread-0thread run51
Thread-0thread run52
Thread-0thread run53
Thread-0thread run54
Thread-0thread run55
Thread-0thread run56
Thread-0thread run57
Thread-0thread run58
Thread-0thread run59
Thread-1thread run18
Thread-1thread run19
Thread-1thread run20
Thread-1thread run21
Thread-1thread run22
Thread-1thread run23
Thread-1thread run24
Thread-1thread run25
Thread-1thread run26
Thread-1thread run27
Thread-1thread run28
Thread-1thread run29
Thread-1thread run30
Thread-1thread run31
Thread-1thread run32
Thread-1thread run33
Thread-1thread run34
Thread-1thread run35
Thread-1thread run36
Thread-1thread run37
Thread-1thread run38
Thread-1thread run39
Thread-1thread run40
Thread-1thread run41
Thread-1thread run42
Thread-1thread run43
Thread-1thread run44
Thread-1thread run45
Thread-1thread run46
Thread-1thread run47
Thread-1thread run48
Thread-1thread run49
Thread-1thread run50
Thread-1thread run51
Thread-1thread run52
Thread-1thread run53
Thread-1thread run54
Thread-1thread run55
Thread-1thread run56
Thread-1thread run57
Thread-1thread run58
Thread-1thread run59这个样式儿的,一个线程都一下子输出那么多呢

2 个回复

倒序浏览
先看看该方法的解释,Thread.yield()方法作用是:暂停当前正在执行的线程对象,并执行其他线程
暂停时什么状态?什么时候截除暂停?
其实yield方法无法实现机会绝对平等。yield()只是使当前线程重新回到可执行状态,允许具有相同优先级的其他线程获得运行机会,所以执行yield()的线程有可能在进入到可执行状态后马上又被执行,即同一现场再次被执行。
而且你多运行几次可以发现,即使t1线程都是第一次先执行,结果也不一样。
机器越好,越明显。你的代码没错,是机子太好了。

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报
谭荣强 发表于 2014-3-23 18:48
先看看该方法的解释,Thread.yield()方法作用是:暂停当前正在执行的线程对象,并执行其他线程
暂停时什么 ...

受教,谢了哈
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马