zoukankan      html  css  js  c++  java
  • java-线程-生产者-消费者

    概述

    在Java中有四种方法支持同步,其中前三个是同步方法,一个是管道方法。
    wait() / notify()方法
    await() / signal()方法
    BlockingQueue阻塞队列方法
    PipedInputStream / PipedOutputStream

    wait() / notify()方法

    public class Storage {
    
        private final int MAX_SIZE = 100;
        private LinkedList<Object> list = new LinkedList<>();
    
        private synchronized void produce(String producer) {
            while (list.size() >= MAX_SIZE) {
                System.out.println("仓库已满,【" + producer + "】: 暂时不能执行生产任务!");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            list.add(new Object());
            this.notifyAll();
        }
    
        private synchronized void consume(String consumer) {
            while (list.size() == 0) {
                System.out.println("仓库已空,【" + consumer + "】: 暂时不能执行消费任务!");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            list.remove();
            this.notifyAll();
        }
    }
    
     public class Producer extends Thread {
            private String producer;
            private Storage storage;
    
            public Producer(Storage storage) {
                this.storage = storage;
            }
    
            @Override
            public void run() {
                while (true) {
                    produce(producer);
                }
            }
    
            public void produce(String producer) {
                storage.produce(producer);
            }
    
            public String getProducer() {
                return producer;
            }
    
            public void setProducer(String producer) {
                this.producer = producer;
            }
    
            public Storage getStorage() {
                return storage;
            }
    
            public void setStorage(Storage storage) {
                this.storage = storage;
            }
        }
    
        public class Consumer extends Thread {
            private String consumer;
            private Storage storage;
    
            public Consumer(Storage storage) {
                this.storage = storage;
            }
    
            @Override
            public void run() {
                while (true) {
                    consume(consumer);
                }
            }
    
            public void consume(String consumer) {
                storage.consume(consumer);
            }
    
            public Storage getStorage() {
                return storage;
            }
    
            public void setStorage(Storage storage) {
                this.storage = storage;
            }
    
            public String getConsumer() {
                return consumer;
            }
    
            public void setConsumer(String consumer) {
                this.consumer = consumer;
            }
        }
    
    

    await() / signal()方法

    public class StorageLock {
        private Lock lock = new ReentrantLock();
        private Condition fullCondition = lock.newCondition();
        private Condition emptyCondition = lock.newCondition();
    
        private final int MAX_SIZE = 10;
    
        private LinkedList<Object> list = new LinkedList<>();
    
        public void produce(String name) {
            try {
                lock.lock();
                while (list.size() == MAX_SIZE) {
                    fullCondition.await();
                    System.out.println("仓库已满," + name + "暂停生产");
                }
                list.add(new Object());
                System.out.println(name + "生产一个,当前仓库容量" + list.size());
                emptyCondition.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    
        public void consume(String name) {
            try {
                lock.lock();
                while (list.size() == 0) {
                    emptyCondition.await();
                    System.out.println("仓库以空," + name + "暂停消费");
                }
                list.remove();
                System.out.println(name + "消费一个,当前仓库容量" + list.size());
                fullCondition.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
    
    

    BlockingQueue阻塞队列方法

    
    public class BlockStorage {
        private final int MAX_SIZE = 10;
        private ArrayBlockingQueue<Object> list = new ArrayBlockingQueue<Object>(MAX_SIZE);
    
        public void produce(String name) throws InterruptedException {
            if(list.size() == MAX_SIZE){
                System.out.println("仓库已满,【" + name + "】: 暂时不能执行生产任务!");
            }
            list.put(new Object());
    
            System.out.println("【" + name + "】:生产了一个产品	【现仓储量为】:" + list.size());
        }
    
        public void consume(String name) throws InterruptedException {
            if(list.size() == 0){
                System.out.println("仓库空,【" + name + "】: 暂时不能执行消费任务!");
            }
            list.take();
    
            System.out.println("【" + name + "】:消费了一个产品	【现仓储量为】:" + list.size());
        }
    
    }
    
  • 相关阅读:
    Lucene教程
    ElasticSearch安装
    MySQL事务
    Java泛型
    Python学习笔记(1)
    @keyframs实现图片gif效果
    glup简单应用---gulpfile.js
    巧用CSS3伪类选择器自定义checkbox和radio的样式
    get传中文参数乱码解决方法
    自定义样式 实现文件控件input[type='file']
  • 原文地址:https://www.cnblogs.com/xckxue/p/8745124.html
Copyright © 2011-2022 走看看