a Thread
for(;;){
  a.send();
  a.wait();
}
b Thread
for(;;){
  b.rec();
  b.notifyall();
}

解决方案 »

  1.   

    package concurrent;public class Consumer
      implements Runnable {
        private Inventory inv;
        public Consumer(Inventory inv) {
            if (inv == null)
                throw new NullPointerException();        this.inv = inv;
        }    public void run() {
            while (true) {
                try {
                    Object item = Consumer.this.inv.get();
                    Thread.sleep( (int) (1000 * Math.random()));
                }
                catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
            }
        }
    }
    ----------------------------------------------------------------------
    package concurrent;public class Producer
      implements Runnable {
        private Inventory inv;
        public Producer(Inventory inv) {
            this.inv = inv;
        }    public void run() {
            while (true) {
                Object item = new Integer( (int) (100 * Math.random()));
                try {
                    this.inv.put(item);
                    Thread.sleep( (int) (5 * 1000 * Math.random()));
                }
                catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
            }
        }
    }
    ----------------------------------------------------------------------
    package concurrent;import java.util.*;public class Inventory {
        private final int size;
        private List items = null;    public Inventory(int size) {
            this.size = size;
            this.items = new ArrayList(size);
        }    public int getSize() {
            return this.size;
        }    public synchronized int getItemCount() {
            return this.items.size();
        }    public synchronized List getItems() {
            return new ArrayList(this.items);
        }    public synchronized String toString() {
            return "Inventory:" + items.toString();
        }    public Object get() throws InterruptedException {
            if (Thread.currentThread().isInterrupted()) {
                throw new InterruptedException();
            }        synchronized (this) {
                try {
                    while (items.size() == 0) {
                        wait();
                    }                int tsize = this.items.size();
                    Object item = this.items.remove(tsize - 1);                System.out.println(
                      "---------------------------------------------");
                    System.out.println(Thread.currentThread() + " CONSUME " + item);
                    System.out.println(this.items.toString());                notify();
                    return item;
                }
                catch (InterruptedException ie) {
                    notify();
                    throw ie;
                }
            }
        }    public void put(Object item) throws InterruptedException {
            if (item == null)
                throw new NullPointerException();        if (Thread.currentThread().isInterrupted()) {
                throw new InterruptedException();
            }        synchronized (this) {
                try {
                    while (this.items.size() == this.size) {
                        wait();
                    }                this.items.add(item);                System.out.println(
                      "---------------------------------------------");
                    System.out.println(Thread.currentThread() + " PRODUCE " + item);
                    System.out.println(this.items.toString());                notify();
                }
                catch (InterruptedException ie) {
                    notify();
                    throw ie;
                }
            }
        }
    }
    ----------------------------------------------------------------------
    package concurrent;public class Main {
        public static void main(String[] args) {
            Inventory inv = new Inventory(10);        Consumer c1 = new Consumer(inv);
            //Consumer c2 = new Consumer(inv);        Producer p1 = new Producer(inv);
            Producer p2 = new Producer(inv);
            Producer p3 = new Producer(inv);        new Thread(c1).start();
            //new Thread(c2).start();        new Thread(p1).start();
            new Thread(p2).start();
            new Thread(p3).start();
        }
    }
      

  2.   

    上面的程序是错的,至少只适合一个消费者和一个生产者,羞ing...
      

  3.   

    Inventory.java改成这样应该可以了吧,版我瞧瞧。
    ---------------------------------------------------------------------
    package concurrent;import java.util.*;public class Inventory {
        private final int size;
        private List items = null;    private Object consumerLock = new Object();
        private Object producerLock = new Object();    private int itemCount;
        private int spaceCount;    public Inventory(int size) {
            if(size<=0) throw new IllegalArgumentException();        this.size = size;
            this.items = new ArrayList(size);        this.itemCount = 0;
            this.spaceCount = size;
        }    public int getSize() {
            return this.size;
        }    public synchronized int getItemCount() {
            return this.items.size();
        }    public synchronized List getItems() {
            return new ArrayList(this.items);
        }    public synchronized String toString() {
            return "Inventory:" + items.toString();
        }    //--------------------------------------------------------------------------
        public Object get() throws InterruptedException {
            if (Thread.currentThread().isInterrupted()) {
                throw new InterruptedException();
            }        Object item = null;
            synchronized (this.consumerLock) {
                try {
                    while (this.itemCount == 0) {
                        this.consumerLock.wait();
                    }
                }
                catch (InterruptedException ie) {
                    this.consumerLock.notify();
                    throw ie;
                }            synchronized (this) {
                    int tsize = this.items.size();
                    item = this.items.remove(tsize - 1);                System.out.println(
                      "---------------------------------------------");
                    System.out.println(Thread.currentThread() +
                                       " CONSUME " + item);
                    System.out.println(this.items.toString());
                }            this.itemCount--;
            }        synchronized (this.producerLock) {
                this.spaceCount++;
                this.producerLock.notify();
            }
            return item;
        }    public void put(Object item) throws InterruptedException {
            if (item == null)
                throw new NullPointerException();        if (Thread.currentThread().isInterrupted()) {
                throw new InterruptedException();
            }        synchronized (this.producerLock) {
                try {
                    while (this.spaceCount == 0) {
                        this.producerLock.wait();
                    }
                }
                catch (InterruptedException ie) {
                    this.producerLock.notify();
                    throw ie;
                }            synchronized (this) {
                    this.items.add(item);                System.out.println(
                      "---------------------------------------------");
                    System.out.println(Thread.currentThread() +
                                       " CONSUME " + item);
                    System.out.println(this.items.toString());
                }            this.spaceCount--;
            }        synchronized (this.consumerLock) {
                this.itemCount++;
                this.consumerLock.notify();
            }
        }
    }