标题:代码如下,想问下为什么第二个线程不能得到时间片!
取消只看楼主
george_vcool
Rank: 2
等 级:新手上路
威 望:3
帖 子:453
专家分:0
注 册:2007-7-23
结帖率:100%
 问题点数:0 回复次数:0 
代码如下,想问下为什么第二个线程不能得到时间片!

class TickCheck{
public static void main(String[] args){
SellThread x=new SellThread();

new Thread(x).start();
try{ Thread.sleep(1);}catch(InterruptedException e){}
x.b=true;
new Thread(x).start();
}
}


class SellThread implements Runnable{
int ticket=1000;
Object o=new Object();
boolean b=false;
public void run(){
if (b)
sell();
else{
synchronized(this){
while(ticket>0){

System.out.println(Thread.currentThread().getName()+" "+ticket);
ticket--;
}
}

}
}
public synchronized void sell(){
while(ticket>0){
System.out.println("sell:"+Thread.currentThread().getName()+" "+ticket);
ticket--;

}
}

搜索更多相关主题的帖子: 线程 代码 时间 
2007-08-22 01:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-164464-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.299682 second(s), 10 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved