问题已经描述在26~40行的注释里,求大神解答import static java.lang.Thread.sleep;class Main
{
    public static void main(String[] args) {
        Resource r=new Resource();
        input input=new input(r);
        output output=new output(r);
        new Thread(input).start();
        new Thread(output).start();
    }
}class Resource
{
    private String name;
    private String sex;
    private boolean flag=true;
    public synchronized void set(String name,String sex) {
        if (!flag)
            try {this.wait();} catch (InterruptedException e) { }
        this.name = name;
        this.sex = sex;
        flag = false;
        this.notify();
        /*
        下面这段代码替换上面那段,为什么等待/唤醒机制失效了?
         if (!flag)
         {
            try {this.wait();} catch (InterruptedException e) {}
         }
        else
        {
            this.name = name;
            this.sex = sex;
            flag = false;
            this.notify();
        }        */    }
    public synchronized void print()
    {
        if(flag==false)
        {
            System.out.println(name+"\t\t"+sex);
            flag=true;
            this.notify();
        }
        else
        {
            try {
                this.wait();
            }
            catch (InterruptedException e){}
        }
    }
}class input implements Runnable
{
    Resource r;
    input(Resource r)
    {
        this.r=r;
    }
    public void run()
    {
        int x = 0;
        while(true)
        {
            if(x==0)
            {
                r.set("mike","male");
            }
            else
            {
                r.set("mary","female");
            }
            x = (x+1)%2;
        }
    }
}class output implements Runnable
{
    Resource r;
    // Object obj = new Object();
    output(Resource r)
    {
        this.r = r;
    }    public void run()
    {
        while(true)
        {
            r.print();
        }
    }
}

解决方案 »

  1.   

    if的作用域,上面一个不论如何都会走到this.notify();而下面要么走if要么走else,新手,若不对请
      

  2.   

    我觉得不是else的问题,而是你flag的值设置反了。
    你的原意是如果名字和性别设定好了,flag 设置成true, 在print里面打印,打印完在设置成false,表明已经打印完成。
    如果flag还是true的时候,就表示还没有打印,set方法就要等。调换一下flag的赋值顺序private boolean flag=false; //还没有设定属性值
    public synchronized void set(String name,String sex) {
            if (!flag)
                try {this.wait();} catch (InterruptedException e) { }
            this.name = name;
            this.sex = sex;
            flag = true;
            this.notify();
     
            /*
            下面这段代码替换上面那段,为什么等待/唤醒机制失效了?
             if (!flag)
             {
                try {this.wait();} catch (InterruptedException e) {}
             }
            else
            {
                this.name = name;
                this.sex = sex;
                flag = true;
                this.notify();
            }
     
            */
     
        }
        public synchronized void print() {
            if (flag)
                try {this.wait();} catch (InterruptedException e) { }
            System.out.println(name + "\t\t" + sex);
            flag = false;
            this.notify();
        }
      

  3.   

    没细看逻辑,首先你得替换前后的代码都不等价,改成下面:
            /*
            下面这段代码替换上面那段,为什么等待/唤醒机制失效了?
             if (!flag)
             {
                try {this.wait();} catch (InterruptedException e) {}
                this.name = name;
                this.sex = sex;
                flag = false;
                this.notify();

             }
            else
            {
                this.name = name;
                this.sex = sex;
                flag = false;
                this.notify();
            }
     
            */
      

  4.   


    兄die if(!flag)
    System.out.println("测试");
    System.out.println("测试");
    两条记录都会打印出来滴 if() 后面没有大括号,
    满足条件只会执行if后一条语句,
    只能这代码写的一点不友好,误导了你。(还有三元表达式,虽然看起来逼格高,一点不友好,要写出通俗易懂的代码)
    这次坑踩了,以后就好了。
      

  5.   

    set唤醒没有设置,再进入就重复设置了
      

  6.   


     /*
            下面这段代码替换上面那段,为什么等待/唤醒机制失效了?
             if (!flag)
             {
                try {this.wait();} catch (InterruptedException e) {}
             }
            else
            {
                this.name = name;
                this.sex = sex;
                flag = false;
                this.notify();
            }
     
            */
    this.notify();都被else了,你觉得嘞.