黑马程序员技术交流社区
标题:
线程中flag的纠结
[打印本页]
作者:
不破大地
时间:
2013-6-18 20:50
标题:
线程中flag的纠结
问题(1):在下面的代码中,为何运行时出错,
public class Threadcomunicate
{
public static void main(String[] args)
{
res r=new res();
Input in=new Input(r);
Output out=new Output(r);
Thread t1=new Thread(in);
Thread t2=new Thread(out);
t1.start();
t2.start();
}
}
class Input implements Runnable
{
private res r;
Object obj=new Object();
Input(res r)
{
this.r=r;
}
public void run()
{
int x=0;
while(true)
{
synchronized(r)
{
if(r.flag)
try {r.wait();}catch(Exception e){}
if(x==0)
{
r.name="mike";
r.sex="man";
}
else
{
r.name="lili";
r.sex="women";
}
}
x=(x+1)%2;
r.flag=true;
r.notify();
}
}
}
class Output implements Runnable
{
res r;
Object obj=new Object();
Output(res r)
{
this.r=r;
}
public void run()
{
while(true)
{
synchronized(r)
{
if(!r.flag)
try {r.wait();}catch(Exception e){}
System.out.println(r.name+".."+r.sex);
r.flag=false;
r.notify();
}
}
}
}
class res
{
String name;
String sex;
boolean flag=false;
}public class 代码体现
{
public static void main(String[] args)
{
res r=new res();
Input in=new Input(r);
Output out=new Output(r);
Thread t1=new Thread(in);
Thread t2=new Thread(out);
t1.start();
t2.start();
}
}
class Input implements Runnable
{
private res r;
Object obj=new Object();
Input(res r)
{
this.r=r;
}
public void run()
{
int x=0;
while(true)
{
synchronized(r)
{
if(r.flag)
try {r.wait();}catch(Exception e){}
if(x==0)
{
r.name="mike";
r.sex="man";
}
else
{
r.name="lili";
r.sex="women";
}
}
x=(x+1)%2;
r.flag=true;
r.notify();
}
}
}
class Output implements Runnable
{
res r;
Object obj=new Object();
Output(res r)
{
this.r=r;
}
public void run()
{
while(true)
{
synchronized(r)
{
if(!r.flag)
try {r.wait();}catch(Exception e){}
System.out.println(r.name+".."+r.sex);
r.flag=false;
r.notify();
}
}
}
}
class res
{
String name;
String sex;
boolean flag=false;
}
问题(2):flag初始化值不是false假吗,那为何毕姥爷解释 这句“if(flag), try {r.wait();}catch(Exception e){}”代码的时候,说如果flag为真,则该线程等待,不是应该为如果flag为假时则线程等待吗?
还望各位高人点拨哈。。。
作者:
ying_1990
时间:
2013-6-18 22:06
代码好乱,好些重复的...
//问题(1):在下面的代码中,为何运行时出错,
public class Threadcomunicate
{
public static void main(String[] args)
{
res r=new res();
Input in=new Input(r);
Output out=new Output(r);
Thread t1=new Thread(in);
Thread t2=new Thread(out);
t1.start();
t2.start();
}
}
class Input implements Runnable
{
private res r;
Object obj=new Object();
Input(res r)
{
this.r=r;
}
public void run()
{
int x=0;
while(true)
{
synchronized(r)
{
if(r.flag)
try {r.wait();}catch(Exception e){}
if(x==0)
{
r.name="mike";
r.sex="man";
}
else
{
r.name="lili";
r.sex="women";
}
r.flag=true;
r.notify();
}
x=(x+1)%2;
}
}
}
class Output implements Runnable
{
res r;
Object obj=new Object();
Output(res r)
{
this.r=r;
}
public void run()
{
while(true)
{
synchronized(r)
{
if(!r.flag)
try {r.wait();}catch(Exception e){}
System.out.println(r.name+".."+r.sex);
r.flag=false;
r.notify();
}
}
}
}
class res
{
String name;
String sex;
boolean flag=false;
}
作者:
王磊
时间:
2013-6-18 22:08
第一个问题
public void run()
{
int x=0;
while(true)
{
synchronized(r)
{
if(r.flag)
try{r.wait();}catch (Exception e) { }
if(x==0)
{
r.name="mike";
r.sex="man";
}
else
{
r.name="lili";
r.sex="women";
}
// } //同步块的主体定义错误,下面对flag和notify()的操作都是两个线程的共有数据,应该写在同步内。
x=(x+1)%2;
r.flag=true;
r.notify();
}//同步主体定义到这里就好了。
}
}
复制代码
第二个问题,if(flag) try {r.wait();}catch(Exception e){} --- flag为true是这个if语句运行主体的条件,只有if主体能运行,才会运行到r.wait()这个线程等待的方法。老师说的没错,如果按楼主说flag为false,则if语句中的主体部分不能运行到,也就没法执行r.wait()方法了。
作者:
ying_1990
时间:
2013-6-18 22:18
出错位置.
r.flag=true;
r.notify();
应该放入同步锁中
if(flag)中flag判断为真,执行if条件语句
if(!flag)中flag判断为假才执行if条件语句
作者:
孙百鑫
时间:
2013-6-24 23:57
楼主您好,由于帖子长时间没有动态,我已经讲帖子改成已解决了,如有问题,可以私聊我。{:soso_e100:}
作者:
崔龙飞
时间:
2013-7-20 14:23
楼主现在解决了没,我和你遇到同样的问题,这两天一直在纠结这个问题,如果你知道了麻烦告诉我哈,痛苦死了。。。
作者:
崔龙飞
时间:
2013-7-20 21:42
我刚刚弄明白你的第二个问题,我觉得这点你不要局限于老师的代码。按照老师的思路,我稍微改动了他的代码,你看看是不是比较容易理解一点
if(r.flag == true) //满足条件表示属性有值,需要输出,输入线程等待
if(r.flag == false) //满足条件表示属性没值,需要赋值,输出线程等待
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2