Java队列mq简单实现
程序员文章站
2022-12-08 19:47:41
mq简单实现(为网络简单版)public class MqServer { public static void main(String[] args) throws InterruptedException { //定义消息队列容器 LinkedBlockingDeque blockingDeque = new LinkedBlockingDeque<>(); //创建生产者线程 Thread...
mq简单实现(为网络简单版)
public class MqServer { public static void main(String[] args) throws InterruptedException { //定义消息队列容器 LinkedBlockingDeque<String> blockingDeque = new LinkedBlockingDeque<>(); //创建生产者线程 Thread producer = new Thread(()->{ //相对列存放内容 for (int msg=0 ; msg<10 ;msg++){ blockingDeque.offer(msg+""); System.out.println(Thread.currentThread().getName()+",向mq服务端存入:"+msg); } },"生产者线程"); //主线程等待生产者投递消息全部完成,开始继续向下执行 producer.start(); producer.join(); //创建消费者线程 new Thread(() -> { //自旋 for (;;){ String mgs = blockingDeque.poll(); if (mgs==null){ return; } System.out.println(Thread.currentThread().getName()+",从mq服务器取出消息:"+mgs); } },"生产者线程").start(); } }
本文地址:https://blog.csdn.net/qq_38009686/article/details/108036322
上一篇: springboot编写批量删除
下一篇: 快速搭建Eureka注册中心