栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

简单易懂的队列消费

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单易懂的队列消费

简易消息队列
  1. 编写queue

    public class MyQueue {
    
        String[] data = new String[3];
        private int putIndex = 0;
        private int getIndex = 0;
        private int size = 0;
    
        public synchronized void put(String element) {
            //当队列存满,需要等待消费者消费
            if (size == data.length) {
                try {
                    System.out.println(Thread.currentThread().getName() + ":满了满了,等下!!!");
                    wait(); //3
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                put(element);
                return;
            }
            put0(element);
    
        }
    
        private void put0(String element) {
            //给队列赋值
            data[putIndex] = element;
            //队列有数据,则唤醒get()的等待线程
            notify();
            System.out.println(Thread.currentThread().getName() + ":put唤醒!!!" + size);
            //每次插入消息后角标加一
            ++putIndex;
            //如果角标达到队列的最大长度,需要赋值为0,从头开始
            if (putIndex == data.length) {
                putIndex = 0;
            }
            //通过size记录每次数组队列在存满消息时等待
            ++size;
        }
    
        public synchronized String get() throws InterruptedException {
            //size=0则没有消息,需要等待
            if (size == 0) {
                try {
                    wait();
                    System.out.println(Thread.currentThread().getName() + ":消费没了,喀喀喀!!!");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return get();
            }
            return get0();
        }
    
        private String get0() throws InterruptedException {
            Thread.sleep(500);
            String result = data[getIndex];
            System.out.println(Thread.currentThread().getName() + ":get唤醒!!!" + size);
            notify();
            ++getIndex;
            if (getIndex == data.length) {
                getIndex = 0;
            }
            --size;
            return result;
        }
    }
    
  2. 编写提供者,向队列存入消息

    public class ProviderThread extends Thread {
    
        private MyQueue myQueue;
    
        public ProviderThread(MyQueue myQueue) {
            this.myQueue = myQueue;
        }
    
        @Override
        public void run() {
            int i = 1;
            while (true) {
                try {
                    String tmp = "线程名:" + Thread.currentThread().getName() + ",提供者提工消息:" + i++;
                    myQueue.put(tmp);
                    System.out.println(tmp);
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
  3. 编写消费者,从队列读取消息

    public class ConsumerThread extends Thread {
    
        private MyQueue myQueue;
    
        public ConsumerThread(MyQueue myQueue){
            this.myQueue = myQueue;
        }
    
        @Override
        public void run() {
            while (true){
                try {
                    String result = myQueue.get();
                    System.out.println("线程名:" + Thread.currentThread().getName() + ",消费者消费的消息:" + result);
                    Thread.sleep(600);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
  4. 编写启动类

    public class QueueMain {
    
        public static void main(String[] args) throws InterruptedException {
            //一个队列,每个线程向队列里面插入消息
            MyQueue myQueue = new MyQueue();
            for (int i = 0; i < 3 ; i++) {
                Thread.sleep(100);
                new ProviderThread(myQueue).start();
            }
            for (int i = 0; i < 1; i++) {
                new ConsumerThread(myQueue).start();
            }
    //        Thread.sleep(10000);
    //        System.exit(0);
        }
    
    }
    
    • 未加递归之前

    • 添加递归之后

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678613.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号