面试官:你的项目哪里用到了 Disruptor?
Disruptor 是一个开源的高性能内存队列由英国外汇交易公司 LMAX 开发的获得了 2011 年的 Oracle 官方的 Dukes Choice Awards(Duke 选择大奖)。Disruptor 提供的功能类似于 Kafka 、RocketMQ 这类分布式队列不过其作为范围是 JVM(内存)。Disruptor 解决了 JDK 内置线程安全队列的性能和内存安全问题。JDK 中常见的线程安全的队列如下队列名字锁是否有界ArrayBlockingQueue加锁ReentrantLock有界LinkedBlockingQueue加锁ReentrantLock有界LinkedTransferQueue无锁CAS无界ConcurrentLinkedQueue无锁CAS无界从上表中可以看出这些队列要不就是加锁有界要不就是无锁无界。而加锁的的队列势必会影响性能无界的队列又存在内存溢出的风险。因此一般情况下我们都是不建议使用 JDK 内置线程安全队列。Disruptor 就不一样了它在无锁的情况下还能保证队列有界并且还是线程安全的。1.广播场景广播场景在我们的开发工作中并不少见比如系统收到上游系统的一个请求消息然后把这个消息发送给多个下游系统来处理。Disruptor 支持广播模式。比如消费者生产的消息由三个消费者来消费public class Broadcast { public static void main(String[] args) throws InterruptedException { int bufferSize 1024; DisruptorLongEvent disruptor new Disruptor(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE); EventHandlerLongEvent consumer1 new LongEventHandler(consumer1); EventHandlerLongEvent consumer2 new LongEventHandler(consumer2); EventHandlerLongEvent consumer3 new LongEventHandler(consumer3); disruptor.handleEventsWith(consumer1, consumer2, consumer3); disruptor.start(); RingBufferLongEvent ringBuffer disruptor.getRingBuffer(); ByteBuffer bb ByteBuffer.allocate(8); for (long l 0; true; l) { bb.putLong(0, l); ringBuffer.publishEvent((event, sequence, buffer) - event.set(buffer.getLong(0)), bb); Thread.sleep(1000); } } }2.日志收集再来看一个日志收集的例子。这里我们假设一个场景业务系统集群有 3 个节点每个节点打印的业务日志发送到 DisruptorDisruptor 下游有 3 个消费者负责日志收集。这里我们需要重新定义一个日志收集处理类代码如下public class LogCollectHandler implements WorkHandlerLongEvent { public LogCollectHandler(String consumer) { this.consumer consumer; } private String consumer; Override public void onEvent(LongEvent event) { System.out.println(consumer: consumer ,Event: event); } }下面这个代码是绑定消费者的代码public static void main(String[] args) throws InterruptedException { int bufferSize 1024; DisruptorLongEvent disruptor new Disruptor(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE); WorkHandlerLongEvent consumer1 new LogCollectHandler(consumer1); WorkHandlerLongEvent consumer2 new LogCollectHandler(consumer2); WorkHandlerLongEvent consumer3 new LogCollectHandler(consumer3); disruptor.handleEventsWithWorkerPool(consumer1, consumer2, consumer3); disruptor.start(); }需要注意的是上面使用的是 Disruptor 的handleEventsWithWorkerPool方法使用的消费者不是EventHandler而是WorkHandler。消费者组里面的消费者如果是WorkHandler那消费者之间就是有竞争的比如一个 Event 已经被 consumer1 消费过那就不再会被其他消费者消费了。消费者组里面的消费者如果是EventHandler那消费者之间是没有竞争的所有消息都会消费。3.责任链责任链这种设计模式我们都比较熟悉了同一个对象的处理有多个不同的逻辑每个逻辑作为一个节点组成责任链比如收到一条告警消息处理节点分为给开发人员发送邮件、给运维人员发送短信、给业务人员发送 OA 消息。Disruptor 支持链式处理消息看下面的示例代码public static void main(String[] args) throws InterruptedException { int bufferSize 1024; DisruptorLongEvent disruptor new Disruptor(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE); EventHandlerLongEvent consumer1 new LongEventHandler(consumer1); EventHandlerLongEvent consumer2 new LongEventHandler(consumer2); EventHandlerLongEvent consumer3 new LongEventHandler(consumer3); disruptor.handleEventsWith(consumer1).then(consumer2).then(consumer3); disruptor.start(); }Disruptor 也支持多个并行责任链下图是 2 条责任链的场景图片这里给出一个示例代码public static void main(String[] args) throws InterruptedException { int bufferSize 1024; DisruptorLongEvent disruptor new Disruptor(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE); EventHandlerLongEvent consumer1 new LongEventHandler(consumer1); EventHandlerLongEvent consumer2 new LongEventHandler(consumer2); EventHandlerLongEvent consumer3 new LongEventHandler(consumer3); EventHandlerLongEvent consumer4 new LongEventHandler(consumer4); EventHandlerLongEvent consumer5 new LongEventHandler(consumer5); EventHandlerLongEvent consumer6 new LongEventHandler(consumer6); disruptor.handleEventsWith(consumer1).then(consumer2).then(consumer3); disruptor.handleEventsWith(consumer4).then(consumer5).then(consumer6); disruptor.start(); }4.多任务协作一个经典的例子我们在泡咖啡之前需要烧水、洗被子、磨咖啡粉这三个步骤可以并行但是需要等着三步都完成之后才可以泡咖啡。当然这个例子可以用 Java 中的 CompletableFuture 来实现代码如下public static void main(String[] args){ ExecutorService executor ...; CompletableFuture future1 CompletableFuture.runAsync(() - { try { washCup(); } catch (InterruptedException e) { e.printStackTrace(); } }, executor); CompletableFuture future2 CompletableFuture.runAsync(() - { try { hotWater(); } catch (InterruptedException e) { e.printStackTrace(); } }, executor); CompletableFuture future3 CompletableFuture.runAsync(() - { try { grindCoffee(); } catch (InterruptedException e) { e.printStackTrace(); } }, executor); CompletableFuture.allOf(future1, future2, future3).thenAccept( r - { System.out.println(泡咖啡); } ); System.out.println(我是主线程); }同样使用 Disruptor 也可以实现这个场景看下面代码public static void main(String[] args) throws InterruptedException { int bufferSize 1024; DisruptorLongEvent disruptor new Disruptor(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE); EventHandlerLongEvent consumer1 new LongEventHandler(consumer1); EventHandlerLongEvent consumer2 new LongEventHandler(consumer2); EventHandlerLongEvent consumer3 new LongEventHandler(consumer3); EventHandlerLongEvent consumer4 new LongEventHandler(consumer4); disruptor.handleEventsWith(consumer1, consumer2, consumer3).then(consumer4); disruptor.start(); }5.多消费者组类比主流消息队列的场景Disruptor 也可以实现多消费者组的场景组间并行消费互不影响组内消费者竞争消息如下图示例代码如下public static void main(String[] args) throws InterruptedException { int bufferSize 1024; DisruptorLongEvent disruptor new Disruptor(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE); WorkHandlerLongEvent consumer1 new LogWorkHandler(consumer1); WorkHandlerLongEvent consumer2 new LogWorkHandler(consumer2); WorkHandlerLongEvent consumer3 new LogWorkHandler(consumer3); WorkHandlerLongEvent consumer4 new LogWorkHandler(consumer4); WorkHandlerLongEvent consumer5 new LogWorkHandler(consumer5); WorkHandlerLongEvent consumer6 new LogWorkHandler(consumer6); disruptor.handleEventsWithWorkerPool(consumer1, consumer2, consumer3); disruptor.handleEventsWithWorkerPool(consumer4, consumer5, consumer6); disruptor.start(); }6.总结通过消费者的灵活组合Disruptor 的使用场景非常丰富。本文介绍了 Disruptor 的 5 个典型使用场景。在选型的时候除了使用场景更多地要考虑到 Disruptor 作为高性能内存队列的这个特点。

相关新闻

【开题答辩全过程】以 社区蔬菜经营平台为例,包含答辩的问题和答案

【开题答辩全过程】以 社区蔬菜经营平台为例,包含答辩的问题和答案

个人简介 一名14年经验的资深毕设内行人,语言擅长Java、php、微信小程序、Python、Golang、安卓Android等 开发项目包括大数据、深度学习、网站、小程序、安卓、算法。平常会做一些项目定制化开发、代码讲解、答辩教学、文档编写、也懂一些降重方面的技巧。 感谢大家…

2026/7/7 11:31:23 阅读更多 →
成埃落定!Oracle 26ai本地部署正式版发布!

成埃落定!Oracle 26ai本地部署正式版发布!

📢📢📢📣📣📣 作者:IT邦德 中国DBA联盟(ACDU)成员,15年DBA工作经验 Oracle、PostgreSQL ACE CSDN博客专家及B站知名UP主,全网粉丝15万 擅长主流Oracle、MySQL、PG、高斯及…

2026/7/8 4:21:36 阅读更多 →
运维老哥熬的夜受的气,转网安全给你补回来!

运维老哥熬的夜受的气,转网安全给你补回来!

运维老哥,你熬的夜、受的气,转行网安真的能“找补”回来 前几天和一哥们儿喝酒,他吐槽:“我在机房守了8年,大年初一被叫回来修数据库,凌晨三点处理过服务器宕机,现在头发秃了,腰也废…

2026/7/7 6:24:48 阅读更多 →

最新新闻

基于群辉 + Docker + Flask 搭建企业内部自动化服务平台

基于群辉 + Docker + Flask 搭建企业内部自动化服务平台

记录一次使用群辉 NAS 搭建企业内部自动化服务平台的实践,包括 Docker 容器部署、自动更新、远程访问以及局域网设备调度。前言随着团队内部自动化需求越来越多,需要一个统一的平台来管理各种自动化流程。相比购买云服务器,群辉 NAS 具有成本…

2026/7/8 7:39:25 阅读更多 →
AKShare金融数据接口库的企业级架构设计与性能调优方案

AKShare金融数据接口库的企业级架构设计与性能调优方案

AKShare金融数据接口库的企业级架构设计与性能调优方案 【免费下载链接】akshare AKShare is an elegant and simple financial data interface library for Python, built for human beings! 开源财经数据接口库 项目地址: https://gitcode.com/gh_mirrors/aks/akshare …

2026/7/8 7:37:24 阅读更多 →
Havenlon |入门认知系列:为什么我们选择海狸作为精神符号

Havenlon |入门认知系列:为什么我们选择海狸作为精神符号

为什么我们选择海狸作为精神符号?它不代表攻击,它代表边界。摘要Havenlon 选择海狸作为精神符号,并不是因为它足够可爱,也不是想给一个严肃的安全系统套上一层轻松的外壳。真正的原因只有一个:海狸是自然界里极少见的工…

2026/7/8 7:37:24 阅读更多 →
RHCA必考的vimtutor命令深度解析:从考试准入到生产自动化

RHCA必考的vimtutor命令深度解析:从考试准入到生产自动化

1. 项目概述:为什么一个“vimtutor”命令值得单独写一篇RHCA级教程?在红帽认证架构师(RHCA)的实战现场,我见过太多人卡在同一个地方——不是不会配Ansible Playbook,不是搞不定OpenShift Operator&#xff…

2026/7/8 7:37:24 阅读更多 →
为什么KMS_VL_ALL_AIO成为Windows和Office激活的终极解决方案?

为什么KMS_VL_ALL_AIO成为Windows和Office激活的终极解决方案?

为什么KMS_VL_ALL_AIO成为Windows和Office激活的终极解决方案? 【免费下载链接】KMS_VL_ALL_AIO Smart Activation Script 项目地址: https://gitcode.com/gh_mirrors/km/KMS_VL_ALL_AIO 在数字化办公环境中,Windows操作系统和Microsoft Office套…

2026/7/8 7:35:24 阅读更多 →
AKShare股票数据获取策略:构建稳定高效的金融数据采集架构

AKShare股票数据获取策略:构建稳定高效的金融数据采集架构

AKShare股票数据获取策略:构建稳定高效的金融数据采集架构 【免费下载链接】akshare AKShare is an elegant and simple financial data interface library for Python, built for human beings! 开源财经数据接口库 项目地址: https://gitcode.com/gh_mirrors/ak…

2026/7/8 7:35:24 阅读更多 →

日新闻

3大核心能力重塑《明日方舟》游戏体验:MAA自动化助手的革命性突破

3大核心能力重塑《明日方舟》游戏体验:MAA自动化助手的革命性突破

3大核心能力重塑《明日方舟》游戏体验:MAA自动化助手的革命性突破 【免费下载链接】MaaAssistantArknights 《明日方舟》小助手,全日常一键长草!| A one-click tool for the daily tasks of Arknights, supporting all clients. 项目地址: …

2026/7/8 0:00:48 阅读更多 →
MyBatis 批量操作深度优化——从 N+1 到批处理的全路径

MyBatis 批量操作深度优化——从 N+1 到批处理的全路径

MyBatis 批量操作深度优化——从 N1 到批处理的全路径 一、从"功能正确"到"性能可接受"——MyBatis 批量操作的三段式进化 MyBatis 在日常增删改查场景中几乎是无感的——实体映射直观、SQL 控制灵活。但当数据量从千级上升到十万级、百万级,许…

2026/7/8 0:00:48 阅读更多 →
工业负载控制方案:TPD2015FN与PIC18F45K22应用解析

工业负载控制方案:TPD2015FN与PIC18F45K22应用解析

1. 工业负载控制方案概述在工业自动化、电机驱动和照明控制等高需求场景中,可靠地控制电感和电阻负载是核心挑战之一。TPD2015FN作为东芝的8通道高端智能功率开关IC,配合PIC18F45K22微控制器,能够构建一套稳定、高效的负载控制系统。这套组合…

2026/7/8 0:02:48 阅读更多 →

周新闻

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱,支持下载视频、番剧等等各类资源 项目地址: https://gitcode.com/GitHub_Trending/bilit/BiliTools …

2026/7/7 14:24:45 阅读更多 →
威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型的陌生现状在忙碌疲惫的一天里,参与了关于混合后量子密码学的讨论,应付端点攻击找茬的人,还参与留言板讨论后,发现“威胁模型”对多数人仍是陌生概念,且多被当作时髦用语。有趣的相关画作有一幅由 Embyr 创作的…

2026/7/7 12:34:47 阅读更多 →
渗透测试入门指南:从零基础到实战环境搭建

渗透测试入门指南:从零基础到实战环境搭建

1. 从“看热闹”到“入门”:我理解的渗透测试到底是什么?每次看到新闻里说某个大公司的数据被“黑”了,或者某个网站被攻击导致服务瘫痪,你是不是和我一样,心里会冒出两个念头:一是“这黑客真厉害”&#x…

2026/7/7 15:59:06 阅读更多 →

月新闻