贴了几个论坛,发现没人能解决这个问题

kingtomcsdn 2018-02-06 12:40:32
问题已经描述在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();
}
}
}
...全文
1986 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我只看了这两段代码, 上面那段执行了等待,必然会被唤醒,下面那段执行了等待一直不会被唤醒,所以下面的代码替换上面后唤醒机制会失效
2B青年弹棉花 2018-07-28
  • 打赏
  • 举报
回复

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


this.notify();都被else了,你觉得嘞.
hf2541 2018-07-27
  • 打赏
  • 举报
回复
set唤醒没有设置,再进入就重复设置了
HQZYX 2018-07-26
  • 打赏
  • 举报
回复

兄die if(!flag)
System.out.println("测试");
System.out.println("测试");
两条记录都会打印出来滴 if() 后面没有大括号,
满足条件只会执行if后一条语句,
只能这代码写的一点不友好,误导了你。(还有三元表达式,虽然看起来逼格高,一点不友好,要写出通俗易懂的代码)
这次坑踩了,以后就好了。
stacksoverflow 2018-07-25
  • 打赏
  • 举报
回复
没细看逻辑,首先你得替换前后的代码都不等价,改成下面:
/*
下面这段代码替换上面那段,为什么等待/唤醒机制失效了?
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();
}

*/
喜氏天狼 2018-02-07
  • 打赏
  • 举报
回复
引用 1 楼 渣渣辉的回复:
if的作用域,上面一个不论如何都会走到this.notify();而下面要么走if要么走else,新手,若不对请
我系轱天乐,你系渣渣辉吗?
soton_dolphin 2018-02-06
  • 打赏
  • 举报
回复
我觉得不是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();
    }
渣渣辉 2018-02-06
  • 打赏
  • 举报
回复
if的作用域,上面一个不论如何都会走到this.notify();而下面要么走if要么走else,新手,若不对请

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧