我想实现生产者一边生产,但是消费者也可以一边消费,代码写的有问题,哪位帮改下。老是生产满了再进行消费。 import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;class Producer implements Runnable
{
 private Container contain = null; public Producer(Container c)
 {
  contain = c;
 } public void run()
 {
  while (true)
  {
   synchronized (contain)
   {
    System.out.println("producer is coming");
    contain.push(new Object());
   }
  }
 }
}class Consumer implements Runnable
{
 private Container contain = null; public Consumer(Container c)
 {
  contain = c;
 } public void run()
 {
  while (true)
  {
   synchronized (contain)
   {
    System.out.println("consumer is coming");
    contain.pop();
   }
  }
 }
}class Container
{
 private ArrayList list = new ArrayList();
 private final int SIZE = 13; public boolean isFull()
 {
  return list.size() == SIZE;
 } public boolean isEmpty()
 {
  return list.isEmpty();
 } public synchronized void push(Object o)
 {
  if(isFull())
  {
   System.out.println("container is full , don't product");
   try
   {
    this.wait();
   }
   catch (InterruptedException e)
   {
    e.printStackTrace();
    this.notifyAll();
   }
  }
  else if(list.size() >= 0 && list.size() < SIZE)
  {
   list.add(o);
   System.out.println(Thread.currentThread().getName()
    + " : push one object , container size : " + list.size());
  }
 } public synchronized void pop()
 {
  if(isEmpty())
  {
   System.out.println("container is empty , please wait");
   try
   {
    this.wait();
   }
   catch (InterruptedException e)
   {
    e.printStackTrace();
    this.notifyAll();
   }
  }
  else if(list.size() > 0 && list.size() <= SIZE)
  {
   list.remove(list.size() - 1);
   System.out.println(Thread.currentThread().getName()
    + " : pop one object , container size : " + list.size());
  }
 }
}public class ProducerConsumer
{ public static void main(String[] args)
 {
  Container c = new Container();
  ExecutorService es = Executors.newCachedThreadPool();
  for(int i = 0; i < 5; i++)
  {
   es.execute(new Producer(c));
   es.execute(new Consumer(c));
  }
 }}

解决方案 »

  1.   

    没有这么费劲去看看JDK6的api,关于LinkedBlockingQueue的说明中有例子可以参照。
      

  2.   

    import java.util.*;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;class Producer implements Runnable
    {
     private Container contain = null; public Producer(Container c)
     {
      contain = c;
     } public void run()
     {
      while (true)
      {
      synchronized (contain)
      {
      System.out.println("producer is coming");
      contain.push(new Object());
      }
      }
     }
    }class Consumer implements Runnable
    {
     private Container contain = null; public Consumer(Container c)
     {
      contain = c;
     } public void run()
     {
      while (true)
      {
      synchronized (contain)
      {
      System.out.println("consumer is coming");
      contain.pop();
      }
      }
     }
    }class Container
    {
     private ArrayList list = new ArrayList();
     private final int SIZE = 13; public boolean isFull()
     {
      return list.size() == SIZE;
     } public boolean isEmpty()
     {
      return list.isEmpty();
     } public synchronized void push(Object o)
     {
      if(isFull())
      {
      System.out.println("container is full , don't product");
      try
      {
      this.wait();
      }
      catch (InterruptedException e)
      {
      e.printStackTrace();
      this.notifyAll();
      }
      }
      else if(list.size() >= 0 && list.size() < SIZE)
      {
      list.add(o);
      System.out.println(Thread.currentThread().getName()
      + " : push one object , container size : " + list.size());
      this.notifyAll();
      }
     } public synchronized void pop()
     {
      if(isEmpty())
      {
      System.out.println("container is empty , please wait");
      try
      {
      this.wait();
      }
      catch (InterruptedException e)
      {
      e.printStackTrace();
      this.notifyAll();
      }
      }
      else if(list.size() > 0 && list.size() <= SIZE)
      {
      list.remove(list.size() - 1);
      System.out.println(Thread.currentThread().getName()
      + " : pop one object , container size : " + list.size());
      this.notifyAll();
      }
     }
    }public class ProducerConsumer
    {
     public static void main(String[] args)
     {
      Container c = new Container();
      ExecutorService es = Executors.newCachedThreadPool();
      for(int i = 0; i < 5; i++)
      {
      es.execute(new Producer(c));
      es.execute(new Consumer(c));
      }
     }}