java 生产者消费者示例代码

2020-08-10  本文已影响0人  kiwilll
public class Test2 {

    private static Test2 instance = new Test2();

    public static Test2 getInstance() {
        return instance;
    }


    public void main() {
        Food food = new Food();
        food.setCount(5);
        food.setStore(100);
        Producer p = new Producer(food);
        Consumer c = new Consumer(food);

        new Thread(c).start();
        new Thread(c).start();
        new Thread(c).start();
        new Thread(c).start();
        new Thread(c).start();

        new Thread(p).start();
        new Thread(p).start();
        new Thread(p).start();
        new Thread(p).start();
        new Thread(p).start();
    }


    class Producer extends Thread {
        private Food food;

        public Producer(Food f) {
            food = f;
        }

        @Override
        public void run() {
            while (food.getStore() > 0) {
                synchronized (food) {
                    try {
                        if (food.getCount() > 5) food.wait();
                        if (food.getCount() > 0) food.notify();
                        if (food.getCount() < 5 && food.getStore() >0) {
                            food.setCount(food.getCount() + 1);
                            food.setStore(food.getStore() - 1);
                        }else
                        {
                            food.notify();
                        }
                        System.out.println("-->Producer:食物:" + food.getCount() +" 库存:"+food.getStore());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    class Consumer extends Thread {

        private Food food;
        public Consumer(Food f) {
            food = f;
        }

        @Override
        public void run() {
            while (food.getStore() > 0) {
                synchronized (food) {
                    try {
                        if (food.getCount() <= 0) food.wait();
                        if (food.getCount() < 5) food.notify();
                        if (food.getCount() > 0) {
                            food.setCount(food.getCount() - 1);
                        }
                        System.out.println("-->Consumer:食物:" + food.getCount() +" 库存:"+food.getStore());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


    public class Food {
        private int count;

        private int store;

        public synchronized int getStore() {
            return store;
        }

        public synchronized void setStore(int store) {
            this.store = store;
        }

        public synchronized int getCount() {
            return count;
        }

        public synchronized void setCount(int x) {
            this.count = x;
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读