zoukankan      html  css  js  c++  java
  • 生产者消费者模型

    import java.util.*;
    import java.util.concurrent.*;
    import java.util.concurrent.locks.*;
    
    public class ConsumerProducer {
        private static Buffer buffer = new Buffer();
    
        public static void main(String[] args) {
            ExecutorService executor = Executors.newFixedThreadPool(2);
            executor.execute(new ProducerTask());
            executor.execute(new ConsumerTask());
            executor.shutdown();
        }
    
        private static class ProducerTask implements Runnable {
            @Override
            public void run() {
                try {
                    int i = 1;
                    while (true) {
                        System.out.println("Producer writes 	" + i);
                        buffer.write(i ++);
                        Thread.sleep((int)(Math.random() * 10000));
                    }
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    
        private static class ConsumerTask implements Runnable {
            public void run() {
                try {
                    while (true) {
                        System.out.println("								Consumer reads:	" + buffer.read());
                        Thread.sleep((int)(Math.random() * 10000));
                    }
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    
        private static class Buffer {
            private static final int CAPACITY = 1;
            private LinkedList<Integer> queue = new LinkedList<>();
    
            private Lock lock = new ReentrantLock();
            private Condition notEmpty = lock.newCondition();
            private Condition notFull = lock.newCondition();
    
            public void write(int value) {
                lock.lock();
                try {
                    while (queue.size() == CAPACITY) {
                        System.out.println("Wait for notFull conditino");
                        notFull.await();
                    }
                    queue.offer(value);
                    notEmpty.signal();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
    
            public int read() {
                int value = 0;
                lock.lock();
                try {
                    while (queue.isEmpty()) {
                        System.out.println("								Wait for notEmpty condition");
                        notEmpty.await();
                    }
                    value = queue.poll();
                    notFull.signal();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                } finally {
                    lock.unlock();
                    return value;
                }
            }
        }
    }
    
    
  • 相关阅读:
    JQuery Ajax 在asp.net中使用小结
    加班对你的提升有多大?
    .net学习笔记---HttpResponse类
    .net学习笔记----HttpRequest类
    MVC学习笔记---MVC生命周期
    MVC学习笔记---MVC生命周期及管道
    .net学习笔记---HttpHandle与HttpModule
    C#学习笔记---修饰符,this关键字和static关键字
    C#学习笔记---如何提高代码逼格
    Linq学习笔记---Linq to Sql之where
  • 原文地址:https://www.cnblogs.com/liulaolaiu/p/11744371.html
Copyright © 2011-2022 走看看