chenchenzucc 2010-10-19 11:57
浏览 230
已采纳

关于notifyAll()问题

不是说notifyAll()是让所有挂起线程恢复的吗?下面代码怎么只恢复了其中t51的线程。。。

class A5 implements Runnable
{ int i=0;
String name;
public void run()
{ while(true)
{ i++;
System.out.println(name+"Z="+i);
if(i==5){
try{ 挂起线程A5();}
catch(Exception e){}

}
try{ Thread.sleep(1000);}
catch(Exception e){}
}
}
public synchronized void 挂起线程A5() throws InterruptedException
{ wait();

}
public synchronized void 恢复线程A5()
{ notifyAll();

}
}

public class Example15
{ public static void main(String args[])
{ int m=0;
A5 t51=new A5(); //线程A5的目标对象
t51.name="张一";

Thread thread=new Thread(t51);
thread.setName(t51.name);

     A5 t52=new A5();    //线程A5的目标对象 
     t52.name="张二";     
     Thread thre=new Thread(t52);
     thre.setName(t52.name);


     thre.start();
     thread.start();

     while(true) 
     { m++;
       System.out.println("我是主线程m="+m);
       if(m==10)
        { System.out.println("让全部线程继续工作");
          try {  
              t51.恢复线程A5(); }                     
          catch(Exception e){} 
          break; 
        }
        try{ Thread.sleep(1000);  } 
        catch(Exception e){}
     }
  }

}

  • 写回答

2条回答 默认 最新

  • iteye_7333 2010-10-19 14:48
    关注

    [code="java"]public class Example15 {
    public static void main(String args[]) {
    int m=0;
    Object oo = new Object();
    A5 t51=new A5(); //线程A5的目标对象
    t51.name="张一";

    t51.synchronizedObject = oo;
    Thread thread=new Thread(t51);
    // thread.setName(t51.name);

        A5 t52=new A5();    //线程A5的目标对象 
        t52.name="张二";     
        t52.synchronizedObject = oo;
        Thread thre=new Thread(t52); 
    

    // thre.setName(t52.name);

        thre.start();
        thread.start(); 
    
        while(true) {
            m++;
            System.out.println("我是主线程m="+m); 
            if(m==10) {
                System.out.println("让全部线程继续工作"); 
                try {t51.恢复线程A5();} catch(Exception e){} 
                break;
            }
            try {Thread.sleep(1000);} catch(Exception e){} 
        }
    } 
    

    }

    class A5 implements Runnable {
    Object synchronizedObject;
    int i=0;
    String name;
    public void run() {
    while(true) {
    i++;
    if(i==5) {
    System.out.println(name+" 挂起.");
    try{挂起线程A5();} catch(Exception e){}

    } else {
    System.out.println(name+"Z="+i);
    }
    try{Thread.sleep(1000);} catch(Exception e){}

            if(i==15) {
                System.out.println(name+" 完成."); 
                break;
            }
        }
    }
    public void 挂起线程A5() throws InterruptedException { 
        synchronizedObject.wait();
    }
    
    public void  恢复线程A5() {
        synchronizedObject.notifyAll();  
    }
    

    }
    [/code]

    1,多线程中常见的问题:wait、notify、notifyAll必须针对同一个同步对象,不显式指定同步对象,同步对象为This,t51的this和t52的this当然不同。

    2,我直接传入的一个相同的同步对象给两个线程,就好了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试