求高人指点是否是死锁的问题~怎么运行为空呢

lengyuqing523 2007-10-24 05:52:13
package lesson5;

class Producer implements Runnable
{
Q q;
public Producer (Q q)
{
this.q=q;
}

public void run()
{
int i =0;

while (true)
{
if(i==0)

q.put("111111","man");

else

q.put("22222", "woman");


i=(i+1)%2;
}


}
}

class Consumer implements Runnable
{
Q q;

public Consumer (Q q)
{
this.q=q;
}

public void run()
{

while (true)

{

q.get();
}

}

}

class Q
{
String name="unknow";
String sex="unknow";
boolean bl;


public synchronized void put(String name,String sex)
{

if(bl);
try{wait();}catch (Exception e){}

this.name=name;

try {Thread.sleep(500);} catch (InterruptedException e) {}
this.sex=sex;
bl=true;
notify();
}
public synchronized void get ()
{

if(!bl);
try{wait();}catch (Exception e){}
System.out.print(name);

System.out.println(":"+sex);
bl=false;
notify();
}
}


class Threadyunxing
{
public static void main(String [] args)
{
Q q=new Q();
new Thread ( new Producer(q)).start();
new Thread ( new Consumer(q)) .start();

}
}
...全文
86 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
a_nuo 2007-10-24
  • 打赏
  • 举报
回复
分给了!
你出现这样问题的原因是
你的代码应没有固定的格式
你最好用一个工具比如JCretor
或者UtrlEdit
lengyuqing523 2007-10-24
  • 打赏
  • 举报
回复
不知道 我给了分没~ 有个框框显示 结贴后显示验证码错误~不知何故
小第出来扎到,分少~多包含
lengyuqing523 2007-10-24
  • 打赏
  • 举报
回复
哇~好强啊 一个分号.......... 十分感谢~要不是高人好眼力 恐怕我这一段时间都不明所以~
多谢~
a_nuo 2007-10-24
  • 打赏
  • 举报
回复

package lesson5;

class Producer implements Runnable
{
Q q;
public Producer (Q q)
{
this.q=q;
}

public void run()
{
int i =0;

while (true)
{
if(i==0)

q.put("111111","man");

else

q.put("22222", "woman");


i=(i+1)%2;
}


}
}

class Consumer implements Runnable
{
Q q;

public Consumer (Q q)
{
this.q=q;
}

public void run()
{

while (true)

{

q.get();
}

}

}

class Q
{
String name="unknow";
String sex="unknow";
boolean bl;


public synchronized void put(String name,String sex)
{

if(bl);
try{wait();}catch (Exception e){}

this.name=name;

try {Thread.sleep(500);} catch (InterruptedException e) {}
this.sex=sex;
bl=true;
notify();
}
public synchronized void get ()
{

if(!bl); //此处多了一个分号,去掉就可以了
try{wait();}catch (Exception e){}
System.out.print(name);

System.out.println(":"+sex);
bl=false;
notify();
}
}


class Threadyunxing
{
public static void main(String [] args)
{
Q q=new Q();
new Thread ( new Producer(q)).start();
new Thread ( new Consumer(q)) .start();

}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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