class Thread2 implements Runnable{
private String name;
private int n=100;
Object obj=new Object();
Thread2(String name){
this.name=name;
}
public void run(){
while(true){
synchronized(obj){
if(n>0)
try{
Thread.sleep(10);
}
catch(InterruptedException e){
}
System.out.println(Thread.currentThread().getName()+""+name+"****"+n--);
}
}
}
}
public class ThreadDemo2{
public static void main(String args[]){
Thread2 t1=new Thread2("yihao");
Thread h1=new Thread(t1);
Thread h2=new Thread(t1);
h1.start();
h2.start();
}
}
是什么原因导致无限循环呀?? |
|