本帖最后由 黄一—一 于 2012-7-11 17:47 编辑
代码如下,为什么执行的结果只有syn1在执行,并且是单线程,求高手解答。
public class test4 {
public static void main(String [] args){
syn1 ss1=new syn1();
syn2 ss2=new syn2();
new Thread(ss1).start();
new Thread(ss1).start();
new Thread(ss2).start();
}
}
class syn1 implements Runnable{
public String str1="";
public void run() {
synchronized(str1){
while(true){
try{Thread.sleep(1);}catch(Exception e){}
System.out.println("syn1:"+Thread.currentThread().getName());
}
}
}
}
class syn2 implements Runnable{
public String str2="";
public void run() {
synchronized(str2){
while(true){
try{Thread.sleep(1);}catch(Exception e){}
System.out.println("syn2:"+Thread.currentThread().getName());
}
}
}
} |
|