您好,父线程在启动子线程后调用wait()等待子线程调用父线程的notify(),但是出现java.lang.IllegalMonitorStateException: current thread not owner,这可能是notify()只能在当前线程中调用,所以子线程不能调用父线程的notify(),我想问一下,如何在子线程中notify父线程,或者实现类似的功能呢?谢谢!入口:
public class MutiliThread {    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       ParentThread parent=new ParentThread();
       parent.start();
    }}父线程:
public class ParentThread extends Thread {    static private int count;    public void run() {        try {
            synchronized (this) {
                while (count < 10) {
                    ChildThread child = new ChildThread();
                    child.setParentthread(this);
                    child.start();
                    System.out.print("parent will wait ");
                    wait();
                    count++;
                }
            }        } catch (Exception ex) {
            ex.printStackTrace();
        }    }
}子线程:
public class ChildThread extends Thread {    static private int count;
    private ParentThread parent;    public void setParentthread(ParentThread parent) {
        this.parent = parent;
    }    public void run() {
        count++;
        try {
            synchronized (this) {
                System.out.print("parent will notify ");
                parent.notifyAll();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        count--;
    }
}

解决方案 »

  1.   


    public class Test {  /**
      * @param args the command line arguments
      */
      public static void main(String[] args) {
      ParentThread parent=new ParentThread();
      parent.start();
      }}class ParentThread extends Thread {  static private int count;  public void run() {  try {
       Object obj = new Object();
      synchronized (obj) {
      while (count < 10) {
      ChildThread child = new ChildThread(obj);
      child.setParentthread(this);
      child.start();
      System.out.println("parent will wait ");
      obj.wait();
      count++;
      System.out.println(count);
      }
      }  } catch (Exception ex) {
      ex.printStackTrace();
      }  }
    }class ChildThread extends Thread {  static private int count;
      private ParentThread parent;
    Object obj;
    public ChildThread(Object obj)
    {
    this.obj = obj;

    }  public void setParentthread(ParentThread parent) {
      this.parent = parent;
      }  public void run() {
      count++;
      try {
      synchronized (obj) {
      System.out.println("parent will notify ");
      obj.notifyAll();
      }
      } catch (Exception ex) {
      ex.printStackTrace();
      }
      count--;
      }
    }
      

  2.   

    wait和notify、notifyAll都是针对相同的对象。你原先代码中wait是在synchronized(父线程对象){wait()}
    但notifyAll是在synchronized(子线程对象){parent.notifyAll()},两个不匹配。应该将synchronized(子线程对象)换为synchronized(parent)public class Test {  /**
      * @param args the command line arguments
      */
      public static void main(String[] args) {
      ParentThread parent=new ParentThread();
      parent.start();
      }}class ParentThread extends Thread {  static private int count;  public void run() {  try {
       Object obj = new Object();
      synchronized (this) {
      while (count < 10) {
      ChildThread child = new ChildThread(obj);
      child.setParentthread(this);
      child.start();
      System.out.println("parent will wait ");
      wait();
      count++;
      System.out.println(count);
      }
      }  } catch (Exception ex) {
      ex.printStackTrace();
      }  }
    }class ChildThread extends Thread {  static private int count;
      private ParentThread parent;
    Object obj;
    public ChildThread(Object obj)
    {
    this.obj = obj;

    }  public void setParentthread(ParentThread parent) {
      this.parent = parent;
      }  public void run() {
      count++;
      try {
      synchronized (parent) {
      System.out.println("parent will notify ");
      parent.notifyAll();
      }
      } catch (Exception ex) {
      ex.printStackTrace();
      }
      count--;
      }
    }
      

  3.   

    www.bmailqy.com 邮件客户端,