news 2026/6/10 15:01:11

面试官:你的项目哪里用到了 Disruptor?

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
面试官:你的项目哪里用到了 Disruptor?

Disruptor 是一个开源的高性能内存队列,由英国外汇交易公司 LMAX 开发的,获得了 2011 年的 Oracle 官方的 Duke's 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; Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE); EventHandler<LongEvent> consumer1 = new LongEventHandler("consumer1"); EventHandler<LongEvent> consumer2 = new LongEventHandler("consumer2"); EventHandler<LongEvent> consumer3 = new LongEventHandler("consumer3"); disruptor.handleEventsWith(consumer1, consumer2, consumer3); disruptor.start(); RingBuffer<LongEvent> 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 个节点,每个节点打印的业务日志发送到 Disruptor,Disruptor 下游有 3 个消费者负责日志收集。

这里我们需要重新定义一个日志收集处理类,代码如下:

public class LogCollectHandler implements WorkHandler<LongEvent> { 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; Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE); WorkHandler<LongEvent> consumer1 = new LogCollectHandler("consumer1"); WorkHandler<LongEvent> consumer2 = new LogCollectHandler("consumer2"); WorkHandler<LongEvent> 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; Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE); EventHandler<LongEvent> consumer1 = new LongEventHandler("consumer1"); EventHandler<LongEvent> consumer2 = new LongEventHandler("consumer2"); EventHandler<LongEvent> 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; Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE); EventHandler<LongEvent> consumer1 = new LongEventHandler("consumer1"); EventHandler<LongEvent> consumer2 = new LongEventHandler("consumer2"); EventHandler<LongEvent> consumer3 = new LongEventHandler("consumer3"); EventHandler<LongEvent> consumer4 = new LongEventHandler("consumer4"); EventHandler<LongEvent> consumer5 = new LongEventHandler("consumer5"); EventHandler<LongEvent> 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; Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE); EventHandler<LongEvent> consumer1 = new LongEventHandler("consumer1"); EventHandler<LongEvent> consumer2 = new LongEventHandler("consumer2"); EventHandler<LongEvent> consumer3 = new LongEventHandler("consumer3"); EventHandler<LongEvent> 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; Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE); WorkHandler<LongEvent> consumer1 = new LogWorkHandler("consumer1"); WorkHandler<LongEvent> consumer2 = new LogWorkHandler("consumer2"); WorkHandler<LongEvent> consumer3 = new LogWorkHandler("consumer3"); WorkHandler<LongEvent> consumer4 = new LogWorkHandler("consumer4"); WorkHandler<LongEvent> consumer5 = new LogWorkHandler("consumer5"); WorkHandler<LongEvent> consumer6 = new LogWorkHandler("consumer6"); disruptor.handleEventsWithWorkerPool(consumer1, consumer2, consumer3); disruptor.handleEventsWithWorkerPool(consumer4, consumer5, consumer6); disruptor.start(); }

6.总结

通过消费者的灵活组合,Disruptor 的使用场景非常丰富。本文介绍了 Disruptor 的 5 个典型使用场景。在选型的时候,除了使用场景,更多地要考虑到 Disruptor 作为高性能内存队列的这个特点。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 14:27:29

IMFBS01现场接口模块

IMFBS01 现场接口模块是工业自动化系统中的信号转换与传递单元&#xff0c;主要用于连接控制系统与现场设备&#xff0c;实现各种输入输出信号的可靠接入与分发。主要功能说明&#xff1a;提供多通道输入/输出接口&#xff0c;用于采集或输出现场信号。支持数字量和模拟量信号的…

作者头像 李华
网站建设 2026/6/10 11:58:11

本站最全网络安全学习路线图(2026版详解版)

近期&#xff0c;大家在网上对于网络安全讨论比较多&#xff0c;想要学习的人也不少&#xff0c;但是需要学习哪些内容&#xff0c;按照什么顺序去学习呢&#xff1f;其实我们已经出国多版本的网络安全学习路线图&#xff0c;一直以来效果也比较不错&#xff0c;本次我们针对市…

作者头像 李华
网站建设 2026/6/10 12:33:49

Github Copilot使用指南

tab 自动补齐代码或者注释ctrl i 内联聊天根据你的要求生成代码agent能够使用执行命令行命令&#xff0c;但是Edit不能

作者头像 李华
网站建设 2026/6/9 1:00:48

MindSpore 大模型训练进阶:高效显存管理 + 增量式断点续训的实践

在千亿参数大模型&#xff08;如 LLaMA-7B/13B&#xff09;的训练场景中&#xff0c;显存瓶颈与训练中断恢复是两大核心痛点 —— 前者直接限制模型规模&#xff0c;后者会导致工业级训练的时间与算力成本翻倍。本次分享基于 MindSpore 的高阶训练特性&#xff0c;构建 “分层显…

作者头像 李华
网站建设 2026/5/31 15:13:44

Spring Boot 3 集成 Apache Calcite:多数据源查询的终极解决方案

熟悉 Spring Boot 3 的开发者&#xff0c;都知道它在简化开发流程、提高开发效率方面的出色表现吧&#xff01;但是&#xff0c;在实际业务场景中&#xff0c;大家肯定都碰到过这样的棘手问题&#xff1a;订单数据存放在 MySQL 里&#xff0c;库存数据在 PostgreSQL 中&#xf…

作者头像 李华