本帖最后由 左华清 于 2012-2-23 11:38 编辑
public class ThreadDemo1 {
public static void main(String[]args){
TestThread tt=new TestThread();
new Thread(tt).start();
tt.str=new String("method");
new Thread(tt).start();
}
}
class TestThread implements Runnable{
int tickets=100;
String str=new String("");
public void run(){
if(str.equals("method")){
while(true){
sail();
}
}else{
while(true){
synchronized(str){
if(tickets>0){
try{Thread.sleep(1);}catch(Exception e){};
System.out.println("run():"+Thread.currentThread().getName()+"-----is sailing ticket"+tickets--);
}
}
}
}
}
public synchronized void sail(){
if(tickets>0){
try{Thread.sleep(1);}catch(Exception e){};
System.out.println("sail():"+Thread.currentThread().getName()+"----is sailing ticket"+tickets--);
}
}
}
请大家帮忙指点下,为什么打印结果静是sail()......................... |