美文网首页
生产者消费者模型

生产者消费者模型

作者: 圣明 | 来源:发表于2021-12-27 14:23 被阅读0次
    package com.wan.situ;
    
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class ThreadLockTest {
    
        public final int MAX_SIZE = 10;
        public final Queue<String> mQueue = new LinkedList<>();
        public boolean mRunState = false;
        public Producer worker;
        public Consumer consumer;
        private static int index;
    
        private final Lock lock = new ReentrantLock();
        private final Condition consumerCondition = lock.newCondition();
        private final Condition producerCondition = lock.newCondition();
    
        public synchronized String getNewIndex() {
            return (index++) + "";
        }
    
        public ThreadLockTest() {
            this.worker = new Producer();
            this.consumer = new Consumer();
        }
    
        public void start() {
            this.mRunState = true;
            this.consumer.start();
            this.worker.start();
        }
    
        class Producer extends Thread {
            @Override
            public void run() {
                while (mRunState) {
                    lock.lock();
                    while (mQueue.size() == MAX_SIZE) {
                        try {
                            System.out.println("生产者 + wait");
                            producerCondition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        Thread.sleep(1000);
                        String p = getNewIndex();
                        if (mQueue.offer(p)) {
                            System.out.println("生产者 + " + p);
                        }
                        consumerCondition.signal();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock.unlock();
                }
            }
        }
    
        class Consumer extends Thread {
            @Override
            public void run() {
                while (mRunState) {
                    lock.lock();
                    while (mQueue.isEmpty()) {
                        try {
                            System.out.println("消费者 - wait");
                            consumerCondition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        Thread.sleep(1000);
                        System.out.println("消费者 - " + mQueue.poll());
                        producerCondition.signal();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock.unlock();
                }
            }
    
        }
    
        public static void main(String[] args) {
            new ThreadLockTest().start();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:生产者消费者模型

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