日期:2014-05-20  浏览次数:20882 次

多线程的通信问题,麻烦大家帮我看看
如果加上try{Thread.sleep(1);}catch(Exception   w){}(24行-27行)要求的打印的效果没问题
张宇,男
李梅,女
张宇,男
李梅,女
但是没有上面那个sleep就不行了
这是为什么呀!!!???请各位帮帮忙~~~谢谢
class   Q
{
String   name   =   "unknown ";
String   sex   =   "unknown ";
boolean   bFull   =   false;


}
class   producer   implements   Runnable
{
Q   q   =   null;

public   producer(Q   q)
{
this.q   =   q;
}
public   void   run()
{
int   i   =0   ;
while(true)
{

synchronized(q)
{
if(q.bFull)
try{wait();}
catch(Exception   w){}
if(i==0)
{
try{Thread.sleep(1);}
catch(Exception   w){}
q.name   =   "张宇 ";
q.sex   =   "男 ";
}
else
{
q.name   =   "李梅 ";
q.sex   =   "女 ";
}
i   =   (i+1)%2;
q.bFull   =   true;
q.notify();
}

}
}
}
class   customer   implements   Runnable
{
Q   q   =   null;
//int   j   =   0;
public   customer(Q   q)
{
this.q   =   q;
}
public   void   run()
{

while(true)
{
synchronized(q)
{
if(!q.bFull)
try{wait();}
catch(Exception   w){}
System.out.println(q.name+   "-----> "   +   q.sex);
q.bFull   =   false;
q.notify();
}
}
}
}

class   TestThread
{
public   static   void   main(String   []   args)
{
Q   q   =   new   Q();
new   Thread(new   producer(q)).start();
new   Thread(new   customer(q)).start();
}
}

------解决方案--------------------
作为生产者和消费者用一个对象做队列肯定是不对的。因为q.notify();有可能唤醒的是生产者也可能是消费者
class Q
{
String name = "unknown ";
String sex = "unknown ";
boolean bFull = false;
}
class ProducterQ{
Q q;
public ProducterQ(Q q){
this.q=q;
}
}
class CustomerQ{
Q q;
public CustomerQ(Q q){
this.q=q;
}
}
class producer implements Runnable
{
ProducterQ pq = null;
CustomerQ cq=null;

public producer(ProducterQ pq,CustomerQ cq)
{
this.pq = pq;
this.cq = cq;
}
public void run()
{
int i =0 ;
while(true)
{

synchronized(pq)
{
while(pq.q.bFull){
try{
pq.wait();
}
catch(Exception w){}
}
if(i==0)
{
pq.q.name = "张宇 ";
pq.q.sex = "男 ";
}
else
{
pq.q.name = "李梅 ";
pq.q.sex = "女 ";
}
i = (i+1)%2;
pq.q.bFull = true;

}
synchronized(cq)
{
cq.notify();