• 多线程的producer和consumer设计模式


    Message消息体

    package com.dwz.concurrency2.chapter14;
    
    public class Message {
        private String data;
    
        public Message(String data) {
            this.data = data;
        }
        
        public String getData() {
            return data;
        }
    }

    MessageQueue消息队列

    package com.dwz.concurrency2.chapter14;
    
    import java.util.LinkedList;
    
    public class MessageQueue {
        private final LinkedList<Message> queue;
        
        private static final int DEFAULT_MAX_LIMIT = 100;
        
        private final int limit;
    
        public MessageQueue() {
            this(DEFAULT_MAX_LIMIT);
        }
        
        public MessageQueue(final int limit) {
            this.limit = limit;
            this.queue = new LinkedList<>();
        }
        
        public void put(final Message message) throws InterruptedException {
            synchronized (queue) {
                while (queue.size() > limit) {
                    queue.wait();
                }
                
                queue.addLast(message);
                queue.notifyAll();
            }
        }
        
        public Message take() throws InterruptedException {
            synchronized (queue) {
                while (queue.isEmpty()) {
                    queue.wait();
                }
                
                Message message = queue.removeFirst();
                queue.notifyAll();
                return message;
            }
        }
        
        public int getMaxLimit() {
            return this.limit;
        }
        
        public int getMessageSize() {
            synchronized (queue) {
                return queue.size();
            }
        }
    }

    ProducerThread生产者

    package com.dwz.concurrency2.chapter14;
    
    import java.util.Random;
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class ProducerThread extends Thread {
        private final MessageQueue messageQueue;
        
        private final static Random random = new Random();
        
        private final static AtomicInteger counter = new AtomicInteger(0);
        
        public ProducerThread(MessageQueue messageQueue, int seq) {
            super("PRODUCER-" + seq);
            this.messageQueue = messageQueue;
        }
        
        @Override
        public void run() {
            while (true) {
                try {
                    Message message = new Message("Message-" + counter.getAndIncrement());
                    messageQueue.put(message);
                    System.out.println(Thread.currentThread().getName() + " put message " + message.getData());
                    Thread.sleep(random.nextInt(1000));
                } catch (InterruptedException e) {
                    break;
                }
            }
        }
    }

    ConsumerThread消费者

    package com.dwz.concurrency2.chapter14;
    
    import java.util.Random;
    
    public class ConsumerThread extends Thread {
        private final MessageQueue messageQueue;
        
        private final static Random random = new Random();
        
        public ConsumerThread(MessageQueue messageQueue, int seq) {
            super("CONSUMER-" + seq);
            this.messageQueue = messageQueue;
        }
        
        @Override
        public void run() {
            while (true) {
                try {
                    Message message = messageQueue.take();
                    System.out.println(Thread.currentThread().getName() + " take a message " + message.getData());
                    Thread.sleep(random.nextInt(1000));
                } catch (InterruptedException e) {
                    break;
                }
            }
        }
    
    }

    测试

    package com.dwz.concurrency2.chapter14;
    
    public class ProducerAndConsumerClient {
        public static void main(String[] args) {
            final MessageQueue messageQueue = new MessageQueue();
            
            new ProducerThread(messageQueue, 1).start();
            new ProducerThread(messageQueue, 2).start();
            new ProducerThread(messageQueue, 3).start();
            
            new ConsumerThread(messageQueue, 1).start();
            new ConsumerThread(messageQueue, 2).start();
        }
    }
  • 相关阅读:
    深入研究java.lang.Runtime类
    全国省市级联数据sql语句 mysql版
    Tesseract——OCR图像识别 入门篇
    Linux数组array基础
    Shell编程中Shift的用法
    对于shell脚本参数获取时的一点小技巧
    RPM是RedHat Package Manager(RedHat软件包管理工具)
    shell 练习
    shell学习总结之自定义函数
    C# 获取汉字的拼音首字母和全拼(含源码)
  • 原文地址:https://www.cnblogs.com/zheaven/p/12160220.html
Copyright © 2020-2023  润新知