class Producer implements Runnable
{
private Q q;
public Producer(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while(true)
{
if(i==0)
q.put("张三","男");
else q.put("李四","女");
i=(i+1)%2;
// synchronized(q)
// { if (q.bFull=true)
//
// try{q.wait();} catch(Exception e) { e.printStackTrace();}
//
// if(i==0)
// {
// q.name="杨双";
// try{Thread.sleep(1);}
// catch(Exception e){}
// q.sex="男";
// }
// else
// {
// q.name="杨玉洁" +
// "";
// try{Thread.sleep(1);}
// catch(Exception e){}
// q.sex="女";
// }
// q.bFull=true;
// q.notify();
// i=(i+1)%2;
// }
}
}
}class Consumer implements Runnable
{
private Q q;
public Consumer(Q q)
{
this.q=q;//this关键字标识当前的实例对象
}
public void run()
{

while(true)
{
// synchronized (q)
// {
// if(!q.bFull)//bfull为假,Consumer线程发布对q的所有权,让出监视器,等待其他线程用notify
//// 方法通知它醒来
// try{q.wait();} catch(Exception e){e.printStackTrace();}
// System.out.println(q.name);
// System.out.println(q.sex);
// q.bFull=false;
// q.notifyAll();//唤醒在q上等待的线程
// }//退出同步块,让出监视器
q.get();
}
}
} class Q{
private String name="unknown";
private String sex="unknown";
private boolean bFull=false;
public synchronized void  put(String name,String sex)
{
if(bFull=true)
try{wait();}catch(Exception e){e.printStackTrace();}
this.name=name;
try{Thread.sleep(10);} catch(Exception e){ e.printStackTrace();}
this.sex=sex;
bFull=true;
notifyAll();
}

public synchronized void get()
{
if(!bFull)
try{wait();}catch(Exception e){e.printStackTrace();}
System.out.print(name);
// try{
// Thread.sleep(1);
// }
// catch(Exception e)
// {
// e.printStackTrace();
// }
System.out.println("的性别是"+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();

}
}

解决方案 »

  1.   

    if(bFull=true)
    改成:
    if(bFull == true) 或者 if(bFull)其它也没发现什么问题。
      

  2.   


    package test.thread;class Q {
    private String name = "unknown";
    private String sex = "unknown";
    private boolean bFull = false; public synchronized void put(String name, String sex) {
    while (bFull)
    try {
    wait();
    } catch (Exception e) {
    e.printStackTrace();
    }
    this.name = name;
    try {
    Thread.sleep(10);
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println("put:" + name + "," + sex);
    this.sex = sex;
    bFull = true;
    notifyAll();
    } public synchronized void get() {
    while (!bFull)
    try {
    wait();
    } catch (Exception e) {
    e.printStackTrace();
    }
    try {
    Thread.sleep(1);
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println("get:" + name + "," + sex);
    bFull = false;
    notify();
    }
    }package test.thread;class Consumer implements Runnable {
    private Q q; public Consumer(Q q) {
    this.q = q;// this关键字标识当前的实例对象
    } public void run() {
    while (true) {
    q.get();
    }
    }
    }
    package test.thread;class Producer implements Runnable {
    private Q q; public Producer(Q q) {
    this.q = q;
    } public void run() {
    int i = 0;
    while (true) {
    if (i == 0)
    q.put("张三", "男");
    else
    q.put("李四", "女");
    i = (i + 1) % 2; }
    }
    }
    package test.thread;public class ThreadCommunication {
    public static void main(String[] args) {
    Q q = new Q();
    new Thread(new Producer(q)).start();
    new Thread(new Consumer(q)).start();
    }
    }
      

  3.   

    问题就出在这个,用debug找出来了
      

  4.   

    class Info
    {
    private String value=null;
    private String key=null;
    private boolean flag=false;
    public synchronized void set(String value,String key)
    {
    if(flag)
    {
    try {
    super.wait();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    this.setKey(key);
    this.setValue(value);
    flag=true;
    super.notify();
    }
    public void setValue(String value)
    {
    this.value=value;
    }
    public void setKey(String key)
    {
    this.key=key;
    }
    public String getKey()
    {
    return this.key;
    }
    public String getValue()
    {
    return this.value;
    }
    public synchronized void get()
    {
    if(!flag)
    {
    try {
    super.wait();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    System.out.println(this.getKey()+"-----------"+this.getValue());
    flag=false;
    super.notify();


    }
    }
    class SetRun implements Runnable
    {
    private Info info=null;
    public SetRun(Info info)
    {
    this.info=info;
    }
    public void run()
    {
    boolean flag=false;
    for(int i=0;i<50;i++)
    {
    if(flag)
    {
    this.info.set("河南大学","henu.edu.cn");
    flag=false;
    }
    else
    {
    this.info.set("百度","www.baidu.com");
    flag=true;

    }
    }
    }
    }
    class GetRun implements Runnable
    {
    private Info info=null;
    public GetRun(Info info)
    {
    this.info=info;
    }
    public void run()
    {
    for(int i=0;i<50;i++)
    {
    this.info.get();
    }
    }
    }
    public class MyThread
    {
    public static void main(String[] args)
    {
    Info info=new Info();
    new Thread(new SetRun(info)).start();
    new Thread(new GetRun(info)).start();


    }}
    楼主要注意 尽量使用boolean 这样习惯一些 i==0可以用boolean代替的