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

刚发的比较凌乱 不好看 再发一个 生产者消费者 求助
消费者:不断取数据
public class Consumer implements Runnable{
private Info info;
public Consumer(Info info){
this.info = info;}
public void run() {
for(int i=0;i<50;i++){
this.info.get();
}}}

生产者:不断变数据
public class Producer implements Runnable {
private Info info = null;
public Producer(Info info){
this.info = info;}
public void run() {
 boolean flag = false;
 for(int i=0;i<50;i++){
 if(!flag){
this.info.set("xielongtao", "basketball");
flag = true;
 }else{
this.info.set("谢龙涛","篮球");
flag = false;}}}}

信息类:生产者改他的数据 ,消费者取他的数据,flag是个标志位初始为false
public class Info {
private String name = "谢龙涛";
private String hobby = "篮球";
private boolean flag = false;
public String getName() {
return name;}
public void setName(String name) {
this.name = name;}
public String getHobby() {
return hobby;}
public void setHobby(String hobby) {
this.hobby = hobby;} 
public  synchronized void  set(String name,String hobby){
System.out.println(Thread.currentThread().getName()+"set");
if (!flag) {
try {
this.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
this.setName(name);
this.setHobby(hobby);
flag = false;
super.notify(); }}
public synchronized void get(){
System.out.println(Thread.currentThread().getName()+"get");
if (flag) {
try {
this.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("name:" + this.getName() + " hobby:"
+ this.getHobby());
flag = true;
super.notify();}}}

main方法
public class SycronizeTest {
public static void main(String[] args) {
Info info = new Info();
Consumer c = new Consumer(info);
Producer p = new Producer(info);
new Thread(c,"consumer").start();
new Thread(p,"producer").start();}}

我想控制线程输出一行中文,然后就输出一行英文
如下  谢龙涛 篮球
      xielongtao basketball
可是运行main方法没有输出这是为什么? 死锁了吗 两个线程都在跑 有唤醒啊消费者为什么没有取到?
------解决方案--------------------
flag = false
consumer线程启动,循环调用info.get(),进不去if{},没机会nofity()
producer线程启动,调用info.set(),进入if{},马上就wait()了,没机会修改flag
------解决方案--------------------
个人浅见:
问题出在set 和get 方法里,以get方法为例,if语句有问题,假设消费线程得到锁,获得运行机会,同时flag 为false 表示可以取,但你的代码什么也没干,方法就结束了。所以这个地方有问题。所以要把可以消费的执行代码放在if语句块后边,不能放在if语句块里面,放在里面的话,只有flag为true时,进入等待,生产线程结束生产,唤醒等待线程时,才能运行。 
所以代码需要稍微改一下:

   public  synchronized void  set(String name,String hobby){
        System.out.println(Thread.currentThread().getName()+"set");
        if (!flag) {// 表示是满的 不能加
            try {
                this.wait();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            this.setName(name);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }//-------------------------------if 在此结束
            this.setHobby(hobby);
            flag = false;//让他取
            super.notify();            
        //}// -------------------------------这不要