写一个通过线程wait / notify通信的生产者消费者代码

static class MangoIce{
        int counter;

        public MangoIce(int counter) {
            this.counter = counter;
        }
    }

    static class Producer implements Runnable
    {
        private final List<MangoIce> barCounter;
        private final int           MAX_CAPACITY;

        public Producer(List<MangoIce> sharedQueue, int size)
        {
            this.barCounter = sharedQueue;
            this.MAX_CAPACITY = size;
        }

        @Override
        public void run()
        {
            int counter = 1;
            while (!Thread.currentThread().isInterrupted())
            {
                try
                {
                    produce(counter++);
                }
                catch (InterruptedException ex)
                {
                    ex.printStackTrace();
                    break;
                }
            }
        }

        private void produce(int i) throws InterruptedException
        {
            synchronized (barCounter)
            {
                while (barCounter.size() == MAX_CAPACITY)
                {
                    System.out.println("吧台满了,冰沙放不下 " + Thread.currentThread().getName() + " 线程等待,当前吧台冰沙数: " + barCounter.size());
                    barCounter.wait();
                }

                Thread.sleep(1000);
                barCounter.add(new MangoIce(i));
                System.out.println("生产第: " + i + "杯冰沙...");
                barCounter.notifyAll();
            }
        }
    }

    static class Consumer implements Runnable
    {
        private final List<MangoIce> barCounter;

        public Consumer(List<MangoIce> sharedQueue)
        {
            this.barCounter = sharedQueue;
        }

        @Override
        public void run()
        {
            while (!Thread.currentThread().isInterrupted())
            {
                try
                {
                    consume();
                } catch (InterruptedException ex)
                {
                    ex.printStackTrace();
                    break;
                }
            }
        }

        private void consume() throws InterruptedException
        {
            synchronized (barCounter)
            {
                while (barCounter.isEmpty())
                {
                    System.out.println("吧台空的,没有冰沙 " + Thread.currentThread().getName() + " 消费者线程等待,当前吧台冰沙数: " + barCounter.size());
                    barCounter.wait();
                }
                Thread.sleep(1000);
                MangoIce i = barCounter.remove(0);
                System.out.println("消费第: " + i.counter + "杯冰沙...");
                barCounter.notifyAll();
            }
        }
    }

    public static void main(String[] args)
    {
        List<MangoIce> taskQueue = new ArrayList<>();
        int MAX_CAPACITY = 5;
        Thread tProducer = new Thread(new Producer(taskQueue, MAX_CAPACITY), "生产者");
        Thread tConsumer = new Thread(new Consumer(taskQueue), "消费者");
        tProducer.start();
        tConsumer.start();
    }

控制台输出:

生产第: 1杯冰沙...
生产第: 2杯冰沙...
生产第: 3杯冰沙...
生产第: 4杯冰沙...
生产第: 5杯冰沙...
吧台满了,冰沙放不下 生产者 线程等待,当前吧台冰沙数: 5
消费第: 1杯冰沙...
消费第: 2杯冰沙...
消费第: 3杯冰沙...
消费第: 4杯冰沙...
消费第: 5杯冰沙...
吧台空的,没有冰沙 消费者 消费者线程等待,当前吧台冰沙数: 0
生产第: 6杯冰沙...
生产第: 7杯冰沙...
生产第: 8杯冰沙...
生产第: 9杯冰沙...
生产第: 10杯冰沙...
吧台满了,冰沙放不下 生产者 线程等待,当前吧台冰沙数: 5
消费第: 6杯冰沙...
消费第: 7杯冰沙...
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章