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

kingtomcsdn 2018-02-06 02:25:31
问题在27~41行的注释里

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)
try {this.wait();} catch (InterruptedException e) { }
System.out.println(name + "\t\t" + sex);
flag = true;
this.notify();
}
}

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;
output(Resource r) {
this.r = r;
}
public void run() {
while (true) {
r.print();
}
}
}
...全文
520 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
soton_dolphin 2018-02-06
  • 打赏
  • 举报
回复
引用 3 楼 soton_dolphin 的回复:
我觉得不是else的问题,而是你flag的值设置反了。 你的原意是如果名字和性别设定好了,flag 设置成true, 在print里面打印,打印完在设置成false,表明已经打印完成。 如果flag还是true的时候,就表示还没有打印,set方法就要等。 调换一下flag的赋值顺序

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();
    }
改了下逻辑
soton_dolphin 2018-02-06
  • 打赏
  • 举报
回复
我觉得不是else的问题,而是你flag的值设置反了。 你的原意是如果名字和性别设定好了,flag 设置成true, 在print里面打印,打印完在设置成false,表明已经打印完成。 如果flag还是true的时候,就表示还没有打印,set方法就要等。 调换一下flag的赋值顺序

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();
    }
soton_dolphin 2018-02-06
  • 打赏
  • 举报
回复
引用 4 楼 soton_dolphin 的回复:
[quote=引用 3 楼 soton_dolphin 的回复:] 我觉得不是else的问题,而是你flag的值设置反了。 你的原意是如果名字和性别设定好了,flag 设置成true, 在print里面打印,打印完在设置成false,表明已经打印完成。 如果flag还是true的时候,就表示还没有打印,set方法就要等。 调换一下flag的赋值顺序

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();
    }
改了下逻辑[/quote]
引用 4 楼 soton_dolphin 的回复:
[quote=引用 3 楼 soton_dolphin 的回复:] 我觉得不是else的问题,而是你flag的值设置反了。 你的原意是如果名字和性别设定好了,flag 设置成true, 在print里面打印,打印完在设置成false,表明已经打印完成。 如果flag还是true的时候,就表示还没有打印,set方法就要等。 调换一下flag的赋值顺序

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();
    }
改了下逻辑[/quote] 看三楼,刚才发错了
kingtomcsdn 2018-02-06
  • 打赏
  • 举报
回复
引用 1 楼 chliang198882 的回复:
把else {} 去掉就可以了
为什么呢?触发wait( ) 后,下面的语句还会执行么?
葛芮拉 2018-02-06
  • 打赏
  • 举报
回复
把else {} 去掉就可以了

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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