问题在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();
}
}
}
{
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();
}
}
}
解决方案 »
- 传入一个object对象,怎么才能用这个对象的get方法获取对象的属性?
- 用js动态为struts1.2<html:form action="">里的action属性赋值
- j2EE面试难题求答案,欢迎大家来讨论
- dom4j 如何获取某节点下的所有节点
- 各位大虾们,快过来帮小弟一把!!!急用的!!!!
- jtable问题,来大牛解决了啊【急求】分不是问题
- 100分求助 rmi 的问题,在windowes 正常,在linux访问拒绝(有policy,关了防火墙)
- 求个dbunit测试时oracle的sequence的问题
- 向高手请教一个CVS的问题,麻烦帮我解决一下
- 将list转化为合并的json,有没有什么好的方案或者想法
- 部署maven springmvc 项目一直失败 在线等 跪求大神
- jsp 显示联合查询数据 oracle 切割字符串
你的原意是如果名字和性别设定好了,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();
}