因为b是先运行的,在ThreadA里,synchronized(b)只有在b运行完了,才进入的

解决方案 »

  1.   

    是啊,主线程的synchronized(b)是在b运行完了之后再进入的,然后synchronized块里面有个b.wait(),此后,没有人Notify他,他怎么醒过来的呢?
      

  2.   

    但假如谁也没有拥有这b对象呢?譬如你说,对下列情形,系统该怎么做?synchronized(this)
        {
          try
          {
            this.wait();
            System.out.println("Completed.Now back to main thread");
          }catch (InterruptedException e){System.out.println("interrupted!");}
        }
      

  3.   

    线程对象有点怪,试试
    public class ThreadA 
    {
      public static void main(String[] args) 
      {
        Object o = new Object();
        ThreadB b=new ThreadB(o);
        b.start();
        System.out.println("b is start....");
        synchronized(o)
        {
          try
          {
     System.out.println("Waiting for b to complete...");
     o.wait();//没有被notify,它为什么还能被唤醒呢?
            System.out.println("Completed.Now back to main thread");
          }catch (InterruptedException e){System.out.println("interrupted!");}
        }
        System.out.println("Total is :"+b.total);
       }
    }
    class ThreadB extends Thread
    {
      Object o;
      ThreadB(Object o) { this.o =o;}
      int total;
      public void run()
      {
        synchronized(o)
        {
          System.out.println("ThreadB is running..");
          for (int i=0;i<100;i++ )
          {
            total +=i;
            System.out.println("total is "+total);
          }
          //o.notify(); //这里没有Notify
        }
      }
    }
      

  4.   

    public class ThreadA 
    {
      public static void main(String[] args) 
      {
        ThreadB b=new ThreadB();
        b.start();
        System.out.println("b is start....");
        synchronized(b)
        {
          try
          {
     System.out.println("Waiting for b to complete...");
     b.wait();//没有被notify,它为什么还能被唤醒呢?
            System.out.println("Completed.Now back to main thread");
          }catch (InterruptedException e){System.out.println("interrupted!");}
        }
        System.out.println("Total is :"+b.total);
       }
    }
    class ThreadB extends Thread
    {
      int total;
      public void run()
      {
        synchronized(this)
        {
          System.out.println("ThreadB is running..");
          for (int i=0;i<100;i++ )
          {
            total +=i;
            System.out.println("total is "+total);
          }
          //notify(); 这里没有Notify
        }
      }
    }
    这个程序可以正常结束

    public class ThreadA 
    {
      public static void main(String[] args) 
      {
        Object o = new Object();
        ThreadB b=new ThreadB(o);
        b.start();
        System.out.println("b is start....");
        synchronized(o)
        {
          try
          {
     System.out.println("Waiting for b to complete...");
     o.wait();//没有被notify,它为什么还能被唤醒呢?
            System.out.println("Completed.Now back to main thread");
          }catch (InterruptedException e){System.out.println("interrupted!");}
        }
        System.out.println("Total is :"+b.total);
       }
    }
    class ThreadB extends Thread
    {
      Object o;
      ThreadB(Object o) { this.o =o;}
      int total;
      public void run()
      {
        synchronized(o)
        {
          System.out.println("ThreadB is running..");
          for (int i=0;i<100;i++ )
          {
            total +=i;
            System.out.println("total is "+total);
          }
          //o.notify(); //这里没有Notify
        }
      }
    }却不能,一个是用this,一个是用一个object,结果不一样,楼上的大哥解释一下啊
      

  5.   

    第一个
    try
          {
     System.out.println("Waiting for b to complete...");
     b.wait();//没有被notify,它为什么还能被唤醒呢?
            System.out.println("Completed.Now back to main thread");
          }catch (InterruptedException e){System.out.println("interrupted!");}
        }
    b已经结束就往下执行try
          {
     System.out.println("Waiting for b to complete...");
     o.wait();//没有被notify,它为什么还能被唤醒呢?
            System.out.println("Completed.Now back to main thread");
          }catch (InterruptedException e){System.out.println("interrupted!");}
        }
        System.out.println("Total is :"+b.total);
       }
    this.o没有指定等待的时间,就一直等下去
      

  6.   

    第一个只有b一个线程,它的wait只是等待自身被执行完以后,就会自动往下执行
    第二个有o,b两个线程,必须用notify或nitifyall唤醒,才能往下执行
    改为:
    class ThreadB extends Thread
    {
      Object o;
      ThreadB(Object o) { this.o =o;}
      int total;
      public void run()
      {
        synchronized(o)
        {
          System.out.println("ThreadB is running..");
          for (int i=0;i<100;i++ )
          {
            total +=i;
            System.out.println("total is "+total);
          }
           o.notify(); 
        }
      }
    }
    即可
      

  7.   

    >>>第一个只有b一个线程,它的wait只是等待自身被执行完以后,就会自动往下执行
    不完全对,试下面例子synchronized(this)
        {
          try
          {
            this.wait();
            System.out.println("Completed.Now back to main thread");
          }catch (InterruptedException e){System.out.println("interrupted!");}
        }
    我的感觉是假如线程对象做为synchronized的对象的话,该线程终止时,会自动发出signal或notify