Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other word's this method behaves exactly as if it simply performs the call wait(0). wait()is not defined in the class Thread ,it is defined in class Objestso there is no diffient between a.wait() and b.wait(),Noted that:
Causes current thread to wait ,you invoke it in ThreadA,so ...
clear to you ?

解决方案 »

  1.   

    1,synchronized(b)表示同步对象b,此后不能调用b的任何被同步的方法,它和ThreadB中的synchronized(this)语句块互斥。
    2,b.wait()表示放弃所在synchronized{....}语句块对对象b的锁定,此时运行b.run(),直到notify().
    3,源代码的写法易引起混淆,事实上,它与一下代码作用一样:
    public static void main(String [] args){
     ThreadB b = new ThreadB();
     b.start();
     b.k();
     }
    }
     class ThreadB extends Thread {
     ...
             public run(){...}
    public void k(){
    synchronized(this){
    try {
      System.out.println("Waiting for b to complete...");
      wait();
      System.out.println("waiting");
    } catch (InterruptedException e) {}
      }
      System.out.println("Total is: " + total);
    }
    ..
      

  2.   

    public static void main(String [] args){
     ThreadB b = new ThreadB();
     b.start();
     b.k();
     }
    }
     class ThreadB extends Thread {
     ...
             public run(){
             synchronized(this){
             for(int i=0;i<100;i++) {
             total += i;
              }
             notify();
             }
            }
      
    public void k(){
    synchronized(this){//如果在运行b.start(),则当前块不可进入,直到notify(),对吗?
    try {
      System.out.println("Waiting for b to complete...");
      wait();//b.k()等,b.start()继续进行,对吗?
    System.out.println("waiting");
    } catch (InterruptedException e) {}
      }
      System.out.println("Total is: " + total);
    }
      

  3.   

    1, 不是的,只有当ThreadB.run()运行到synchronized(this)以下即取得对象锁则ThreadB.k()中的synchronized语句块无法进入,
    2,对