标题: 这个能运行吗? [打印本页] 作者: 邓熊财 时间: 2015-3-23 12:54 标题: 这个能运行吗? class Num
{ private String name;
private String age;
private boolean flag = false;
public synchronized void set(String name , String age)
{
if(flag)
{
try{this.wait();}catch(Exception e){}
this.name = name;
this.age = age;
flag = true;
this.notify();
}
}
public synchronized void out()
{
if(!flag)
{ try{this.wait();}catch(Exception e){}
System.out.println(name+"name"+",,,,,,"+age+"age");
flag = false;
this.notify();
}
}
}
class Nu implements Runnable
{ Num bbs;
Nu(Num bbs)
{
this.bbs=bbs;
}
public void run()
{
int x = 0;
while(true)
{
if (x==0)
{
bbs.set("名字","性别");
}else
{
bbs.set("lisi","坏人");
}
x = (x+1)%2;
}
}
}
class Num1 implements Runnable
{
private Num bbs;
Num1(Num bbs)
{
this.bbs=bbs;
}
public void run ()
{ while(true)
{
bbs.out();
}
}
}
class YouHua
{
public static void main(String[] args)
{
Num bbs = new Num();
new Thread(new Nu(bbs)).start();
new Thread(new Num1(bbs)).start();
}
} 作者: 邓熊财 时间: 2015-3-23 13:24
为什么我运行的时候是撒也没有呢?作者: 关山明月 时间: 2015-3-23 14:18
Num 类中的set方法和out方法中的判断有问题,if判断后面不要大括号
public synchronized void set(String name , String age)