美文网首页
java 生产者消费者示例代码

java 生产者消费者示例代码

作者: kiwilll | 来源:发表于2020-08-10 17:42 被阅读0次
    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;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:java 生产者消费者示例代码

          本文链接:https://www.haomeiwen.com/subject/rzwjdktx.html