程序一开始就停止了,发现好像是consumer的那个线程先运行,而且一直在等待中。
为什么不是Producer的线程先运行呢???
以上是个人得出的结论,但不敢肯定是不是这样的情况,请高手指教!!!
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("zhangsan","male");
else
q.put("lisi","female");

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
{
private String name="unknow";
private String sex="unknow";
boolean bFull=false;

public synchronized void put(String name,String sex)
{
if(bFull);
try{wait();} catch(Exception e){}
this.name=name;
this.sex=sex;
bFull=true;
notify();
}

public synchronized void get()
{
if(!bFull)
try{wait();} catch(Exception e){}
System.out.print(this.name);
System.out.println(":" + this.sex);
bFull=false ;
notify();

}
}class ThreadCommunication
{

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


}
}