package duo_xian_cheng;
class dui_xiang extends Thread{
String name_1;
dui_xiang(String a ){
super();
name_1=a;
}
synchronized void fun_1(){
try{for(int i=0;i<10;i++)
{System.out.println(name_1+ "fun_1");
sleep(1000);}
}catch(InterruptedException e){
System.out.println( "InterruptedException:fun_1");
}
System.out.println( name_1+"fun_1");
}
synchronized void fun_2(){
try{for(int i=0;i<10;i++)
{System.out.println(name_1+ "fun_2");
sleep(1000);}
}catch(InterruptedException e){
System.out.println( "InterruptedException:fun_2");
}System.out.println( name_1+"fun_2");
}
synchronized void fun_3(){
try{for(int i=0;i<10;i++)
{System.out.println(name_1+ "fun_3");
sleep(1000);}
}catch(InterruptedException e){
System.out.println( "InterruptedException:fun_3");
}System.out.println( name_1+"fun_3");
}
public synchronized void run(){
fun_1();
fun_2();
fun_3();
}
}
public class dan_dui_xiang {
public static void main(String[] args) {
Thread a=new dui_xiang("線程1");
Thread b=new dui_xiang("線程2");
a.start();
b.start();
}
}
Thread a=new dui_xiang("線程1");
Thread b=new dui_xiang("線程2");
a.start();
b.start();
這裡的synchronized 只對調用同一個類實例的方法有同步,你這是兩個類實例,a,b,所以不起作用