Java多线程实现生产者消费者模型

Posted optor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java多线程实现生产者消费者模型相关的知识,希望对你有一定的参考价值。

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Solution {

    static class Queue<T> {

        private int capacity;
        private Object[] elementData;

        private int size;
        private int consumePoint;
        private int productPoint;

        private Lock lock;
        private Condition emptyCondition;
        private Condition fullCondition;

        public Queue(int capacity) {
            this.capacity = capacity;
            elementData = new Object[capacity];

            size = 0;
            consumePoint = 0;
            productPoint = 0;

            lock = new ReentrantLock();
            emptyCondition = lock.newCondition();
            fullCondition = lock.newCondition();
        }

        @SuppressWarnings("unchecked")
        public T get() {
            Object data;
            lock.lock();
            try {
                while (size == 0) {
                    try {
                        emptyCondition.await();
                    } catch (InterruptedException ignored) {
                    }
                }

                if (consumePoint == capacity) {
                    consumePoint = 0;
                }
                data = elementData[consumePoint++];
                size--;
                fullCondition.signalAll();
            } finally {
                lock.unlock();
            }
            return (T) data;
        }

        public void put(T data) {
            lock.lock();
            try {
                while (size == capacity) {
                    try {
                        fullCondition.await();
                    } catch (InterruptedException ignored) {
                    }
                }

                if (productPoint == capacity) {
                    productPoint = 0;
                }
                elementData[productPoint++] = data;
                size++;
                emptyCondition.signalAll();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException, IOException {
        Queue<Integer> queue = new Queue<>(5);

        int consumeThreadCount = 5, productThreadCount = 5;
        List<Thread> consumeThreads = new ArrayList<>(consumeThreadCount);
        List<Thread> productThreads = new ArrayList<>(productThreadCount);

        for (int i = 0; i < 5; i++) {
            Thread consumeThread = new Thread(() -> {
                while (true) {
                    Integer num = queue.get();
                    System.out.println(String.format("[%s] GET %d", Thread.currentThread().getName(), num));
                }
            });
            consumeThread.setName(String.format("consumeThread-%d", i));
            consumeThreads.add(consumeThread);

            Thread productThread = new Thread(() -> {
                while (true) {
                    int num = ThreadLocalRandom.current().nextInt(100);
                    queue.put(num);
                    System.out.println(String.format("[%s] PUT %d", Thread.currentThread().getName(), num));
                }
            });
            productThread.setName(String.format("productThread-%d", i));
            productThreads.add(productThread);
        }

        for (int i = 0; i < 5; i++) {
            consumeThreads.get(i).start();
            productThreads.get(i).start();
        }

        System.in.read();

    }
}

以上是关于Java多线程实现生产者消费者模型的主要内容,如果未能解决你的问题,请参考以下文章

java多线程笔记--生产者消费组模型

Java多线程:BlockingQueue实现生产者消费者模型

Java多线程实现生产者消费者模型

java 多线程 22 :生产者/消费者模式 进阶 利用await()/signal()实现

JAVA 多线程制作大球吃小球 实现球的自动生成及运动 生产消费模型

JavaWeb 基础知识 --多线程(阻塞队列+生产消费者模型)