美文网首页
生产者-消费者

生产者-消费者

作者: 会飞的蜗牛F | 来源:发表于2020-10-13 08:52 被阅读0次

    1、仓库类,包含生产方法和消费方法

    package pro_consumer_synchronized;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author feng
     * @date:2019/12/26
     * @description 仓库
     **/
    public class Storage {
        private static final int Max = 10;
        private static Object lock = new Object();
        List<Object> list = new ArrayList<>();
    
        public void produce(){
           synchronized (lock){
               //超过了就等待
               while (list.size()+1> Max){
                   System.out.println("【生产者" + Thread.currentThread().getName()
                           + "】仓库已满");
                   try {
                       lock.wait();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
               list.add(new Object());
               System.out.println("【生产者" + Thread.currentThread().getName()
                       + "】生产一个产品,现库存" + list.size());
               lock.notifyAll();
           }
        }
    
        public void consumer(){
            synchronized (lock){
                while (list.size() == 0){
                    System.out.println("【消费者" + Thread.currentThread().getName()
                            + "】仓库为空");
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
               list.remove(0);
                System.out.println("【消费者" + Thread.currentThread().getName()
                        + "】消费一个产品,现库存" + list.size());
                lock.notifyAll();
            }
        }
    }
    
    

    2、生产者线程

    package pro_consumer_synchronized;
    
    /**
     * @author feng
     * @date:2019/12/26
     * @description
     **/
    public class Producer implements Runnable {
    
        private Storage storage;
        public  Producer(Storage storage){
            this.storage = storage;
        }
        @Override
        public void run() {
            while (true){
                try {
                    Thread.sleep(1000);
                    storage.produce();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    

    3、消费者线程

    package pro_consumer_synchronized;
    
    /**
     * @author feng
     * @date:2019/12/26
     * @description
     **/
    public class Consumer implements Runnable {
    
        private Storage storage;
        public Consumer(Storage storage){
            this.storage = storage;
        }
        @Override
        public void run() {
            while (true){
                try {
                    Thread.sleep(2000);
                    storage.consumer();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
        }
    }
    
    

    4、主线程

    package pro_consumer_synchronized;
    
    /**
     * @author feng
     * @date:2019/12/26
     * @description  生成 消费者
     **/
    public class Main {
        public static void main(String[] args) {
            Storage s  =new Storage();
            new Thread(new Producer(s)).start();
            new Thread(new Producer(s)).start();
            new Thread(new Producer(s)).start();
            new Thread(new Consumer(s)).start();
            new Thread(new Consumer(s)).start();
            new Thread(new Consumer(s)).start();
        }
    }
    
    

    另外还可以采用ReentrantLock

     private  ReentrantLock lock = new ReentrantLock();
        private Condition full = lock.newCondition();
        private Condition empty = lock.newCondition();
    

    采用blockingDeque

     LinkedBlockingDeque<Object> blockingDeque = new LinkedBlockingDeque<>(10);
    

    相关文章

      网友评论

          本文标题:生产者-消费者

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