问题已经描述在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();
}
}
}
{
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();
}
}
}
解决方案 »
- 多线程问题
- java 如何调用导出数据并打包成zip文件
- 给定一段字符串,其中有a-Z的52个字母组成,其中单词是靠回车或制表符或空格分割,请统计这段文件中有多少个单词
- 菜鸟求助!将arraylist中的查询结果插入数据库该怎么做啊
- 为什么paintComponent(Graphics g)不能立即得到Image.getWidth()的值
- 请教:如何播放wav格式的音频。。。。
- Document接口的使用请教.
- 在使用微软Java包时,遇到的问题.
- 想写一篇关于数据库连接池的文章在期刊上发表,给点意见!waitting...
- 在java中如何生成可单独安装运行的应用程序呀?就像.exe一样!
- 这个要怎么写
- 有谁知道<br data-filtered="filtered">是什么
你的原意是如果名字和性别设定好了,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();
}
/*
下面这段代码替换上面那段,为什么等待/唤醒机制失效了?
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();
}
*/
兄die if(!flag)
System.out.println("测试");
System.out.println("测试");
两条记录都会打印出来滴 if() 后面没有大括号,
满足条件只会执行if后一条语句,
只能这代码写的一点不友好,误导了你。(还有三元表达式,虽然看起来逼格高,一点不友好,要写出通俗易懂的代码)
这次坑踩了,以后就好了。
/*
下面这段代码替换上面那段,为什么等待/唤醒机制失效了?
if (!flag)
{
try {this.wait();} catch (InterruptedException e) {}
}
else
{
this.name = name;
this.sex = sex;
flag = false;
this.notify();
}
*/
this.notify();都被else了,你觉得嘞.