qq_33816944 2016-06-07 03:24 采纳率: 50%
浏览 1179
已采纳

为什么两个方法没有同步啊?

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();
  
 }
 
  
}

  • 写回答

2条回答 默认 最新

  • danielinbiti 2016-06-07 03:30
    关注
    Thread a=new dui_xiang("线程1");
      Thread b=new dui_xiang("线程2");
            a.start();
            b.start();
                    这里的synchronized 只对调用同一个类实例的方法有同步,你这是两个类实例,a,b,所以不起作用
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line