多线程-多生产者多消费者模型

2018-05-04  本文已影响0人  谢谢那些曾经丶

多线程中比较经典的就是生产者消费者模式了,很多复杂的模式也是在这个基础上演变的。里面也又很多的小知识点。

生产者消费者代码
public class AssemblyLine<T> {
//存放生产数据的队列
private final List<T> queue;
//最大生产值
private final int queueMaxSize;

public AssemblyLine(List<T> messageQueue, int max) {
    this.queue = messageQueue;
    this.queueMaxSize = max;
}
//生产者
public void produceMessage(T message) {
    synchronized (queue){
        while (queue.size()>=queueMaxSize){
            try {
                queue.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+":produce:"+message);
        queue.add(message);
        queue.notifyAll();
    }
}
//消费者
public void consumeMessage(){
    synchronized (queue){
        while (queue.isEmpty()){
            try {
                queue.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+":consume:"+ queue.remove(0));
        queue.notifyAll();
    }
}
执行代码
public class Test {
public static void main(String[] args) {
    final List<String> messageQueue=new LinkedList<>();
    final int MAX_PRODUCE_SIZE=20;
    AssemblyLine<String> assemblyLine = new AssemblyLine<>(messageQueue,   MAX_PRODUCE_SIZE);
    Stream.of("produce1","produce2","produce3").forEach(s ->
        new Thread(()-> {
            while (true){
                assemblyLine.produceMessage(UUID.randomUUID().toString());
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },s).start()
    );
    Stream.of("consume1","consume2").forEach(s ->
        new Thread(()->{
            while (true){
                assemblyLine.consumeMessage();
                try {
                    TimeUnit.MILLISECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },s).start()
    );
}
总结
上一篇 下一篇

猜你喜欢

热点阅读