黑马程序员技术交流社区

标题: 生产者消费者。。。。 [打印本页]

作者: 王飞    时间: 2012-7-21 19:48
标题: 生产者消费者。。。。
package 线程间的通信;

public class 生产者和消费者 {


        public static void main(String[] args) {
                 

                Shangp sp = new Shangp();
                Input i  = new Input(sp);
                Output o = new Output(sp);
               
                Thread t1 = new Thread(i);
                Thread t2 = new Thread(o);
               
                t1.start();
                t2.start();
        }

}



class Input implements Runnable
{
        Shangp s;
        public Input(Shangp s)
        {
                this.s = s;
        }

        public void run()
        {
                while(true)
                {
                        try
                        {
                                s.set("商品");
                        }
                         catch (Exception e) {
                                 
                        }
                }

        }
       
}

class Output implements Runnable
{
        Shangp s ;
        public Output(Shangp s)
        {
                this.s = s;
        }
        public void run()
        {
                while(true)
                {
                        try
                        {
                                        s.get();
                        }
                        catch (Exception e) {
                         
                        }
                         
                }

        }
}


class Shangp
{
        String name ;
        String xinxi;
        int count = 0;
        boolean flag = false;

        public synchronized void set(String name)throws InterruptedException
        {
               
                while(!flag)//-----为true时开始生产
                {       
                        if(flag)wait();
                        this.name = name+count+++"-------";
                        System.out.println("生产"+count);
                        flag = true;
                        notify();
                }                 
                 
        }
        public synchronized void get() throws InterruptedException
        {
                while(flag)//-----为false时开始消费
                {        if(flag)wait();   //为false就直接放弃执行权
                        System.out.println(".....消费"+count);
                        flag = false;   //切换标识
                        notify();  //唤醒线程
                }
                   //求救:为什么我这里只生产了1,就不在生产了啊。。。。
        }
}
作者: 王飞    时间: 2012-7-21 19:52


作者: 邓超军    时间: 2012-7-21 20:52
while(flag)//-----为false时开始消费
                {        if(flag)wait();   //为false就直接放弃执行权//这一个语句应该为if(!flag)wait();
                        System.out.println(".....消费"+count);
                        flag = false;   //切换标识
                        notify();  //唤醒线程
                }

既然你循环和线程判定条件都用flag,那么就应该让他们相反。
作者: 郭凯敏    时间: 2012-7-22 06:21
你生产完毕将标识符设为了true,在消费者中你的判断条件是当标识为真就休眠.....所以线程阻塞了,只需要把消费者的标识符改为!flag即可.....试试吧

public class 生产者和消费者 {
public static void main(String[] args) {
  Shangp sp = new Shangp();
  Input i = new Input(sp);
  Output o = new Output(sp);
  Thread t1 = new Thread(i);
  Thread t2 = new Thread(o);
  t1.start();
  t2.start();
}
}
class Input implements Runnable {
Shangp s;
public Input(Shangp s) {
  this.s = s;
}
public void run() {
  while (true) {
   try {
    s.set("商品");
   } catch (Exception e) {
   }
  }
}
}
class Output implements Runnable {
Shangp s;
public Output(Shangp s) {
  this.s = s;
}
public void run() {
  while (true) {
   try {
    s.get();
   } catch (Exception e) {
   }
  }
}
}
class Shangp {
String name;
String xinxi;
int count = 0;
boolean flag = false;
public synchronized void set(String name) throws InterruptedException {
  while (!flag)// -----为true时开始生产
  {
   if (flag)
    wait();
   this.name = name + count++ + "-------";
   System.out.println("生产" + count);
   flag = true;//-----------------------------------------------------1.
   notify();
  }
}
public synchronized void get() throws InterruptedException {
  while (flag)// -----为false时开始消费
  {
   if (!flag)//这个地方你的标识符错误,改成这样就可以了。试试吧----2
    wait(); //
   System.out.println(".....消费" + count);
   flag = false;
   notify();
  }

}
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2