本帖最后由 xingqi10 于 2012-11-20 11:53:51 编辑

解决方案 »

  1.   

    把Consumer类的
     p.setIsEmpty(Boolean.TRUE);
     p.notify();
    这两行代码放到synchronized块中:public class Consumer implements Runnable{
    private Person p;
     
        public Consumer(Person p) {
            this.p = p;
        }
     
        public void run() {
            for (int i = 0; i < 100; i++) {
                synchronized (p) {
                    if (!p.getIsEmpty().equals(Boolean.FALSE)) {
                        try {
                            p.wait();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    String name = p.getName();
                    String sex = p.getSex();
                    System.out.println(name + "--->" + sex);
                    p.setIsEmpty(Boolean.TRUE);
                    p.notify();
                }
            }
        }
    }
      

  2.   

    Consumer这个类你能编译通过?
    以下两行代码没有在同步块里(synchronized (p))
    p.setIsEmpty(Boolean.TRUE);
    p.notify();