原文地址:http://www.symphonious.net/2011/07/11/lmax-disruptor-high-performance-low-latency-and-simple-too/

The LMAX disruptor is an ultra-high performance, low-latency message exchange between threads. It's a bit like a queue on steroids (but quite a lot of steroids) and is one of the key innovations used to make the LMAX exchange run so fast. There is a rapidly growing set of information about what the disruptor is, why it's important and how it works – a good place to start is the list of articles and for the on-going stuff, follow LMAX Blogs. For really detailed stuff, there's also the white paper (PDF).

While the disruptor pattern is ultimately very simple to work with, setting up multiple consumers with the dependencies between them can require a bit too much boilerplate code for my liking. To make it quick and easy for 99% of cases, I've whipped up a simple DSL for the disruptor pattern. For example, to wire up a "diamond pattern" of consumers:

(Image blatantly stolen from Trisha Gee's excellent series explaining the disruptor pattern)

In this scenario, consumers C1 and C2 can process entries as soon as the producer (P1) puts them on the ring buffer (in parallel). However, consumer C3 has to wait for bothC1 and C2 to complete before it processes the entries. In real life this might be because we need to both journal the data to disk (C1) and validate the data (C2) before we do the actual business logic (C3).

With the raw disruptor syntax, these consumers would be created with the following code:

Executor executor = Executors.newCachedThreadPool();
BatchHandler handler1 = new MyBatchHandler1();
BatchHandler handler2 = new MyBatchHandler2();
BatchHandler handler3 = new MyBatchHandler3()
RingBuffer ringBuffer = new RingBuffer(ENTRY_FACTORY, RING_BUFFER_SIZE);
ConsumerBarrier consumerBarrier1 = ringBuffer.createConsumerBarrier();
BatchConsumer consumer1 = new BatchConsumer(consumerBarrier1, handler1);
BatchConsumer consumer2 = new BatchConsumer(consumerBarrier1, handler2);
ConsumerBarrier consumerBarrier2 =
ringBuffer.createConsumerBarrier(consumer1, consumer2);
BatchConsumer consumer3 = new BatchConsumer(consumerBarrier2, handler3);
executor.execute(consumer1);
executor.execute(consumer2);
executor.execute(consumer3);
ProducerBarrier producerBarrier =
ringBuffer.createProducerBarrier(consumer3);

We have to create our actual handlers (the two instances of MyBatchHandler), plus consumer barriers, BatchConsumer instances and actually execute the consumers on their own threads. The DSL can handle pretty much all of that setup work for us with the end result being:

Executor executor = Executors.newCachedThreadPool();
BatchHandler handler1 = new MyBatchHandler1();
BatchHandler handler2 = new MyBatchHandler2();
BatchHandler handler3 = new MyBatchHandler3();
DisruptorWizard dw = new DisruptorWizard(ENTRY_FACTORY, RING_BUFFER_SIZE, executor);
dw.consumeWith(handler1, handler2).then(handler3);
ProducerBarrier producerBarrier = dw.createProducerBarrier();

We can even build parallel chains of consumers in a diamond pattern: 

(Thanks to Trish for using her fancy graphics tablet to create a decent version of this image instead of my original finger painting on an iPad…)

dw.consumeWith(handler1a, handler2a);
dw.after(handler1a).consumeWith(handler1b);
dw.after(handler2a).consumeWith(handler2b);
dw.after(handler1b, handler2b).consumeWith(handler3);
ProducerBarrier producerBarrier = dw.createProducerBarrier();

The DSL is quite new so any feedback on it would be greatly appreciated and of course feel free to fork it on GitHub and improve it.

转载于:https://www.cnblogs.com/davidwang456/p/4559169.html

LMAX Disruptor – High Performance, Low Latency and Simple Too 转载相关推荐

  1. The LMAX disruptor Architecture--转载

    原文地址: LMAX is a new retail financial trading platform. As a result it has to process many trades wit ...

  2. Sparrow - Distributed, Low Latency Scheduling

    http://www.cs.berkeley.edu/~matei/papers/2013/sosp_sparrow.pdf http://www.eecs.berkeley.edu/~keo/tal ...

  3. Log4j2 - java.lang.NoSuchMethodError: com.lmax.disruptor.dsl.Disruptor

    问题 项目使用了log4j2,由于使用了全局异步打印日志的方式,还需要引入disruptor的依赖,最后使用的log4j2和disruptor的版本依赖如下: <dependency>&l ...

  4. low latency playback、deep buffer playback、compressed offload playback的区别

    1,音频播放 Android系统audio框架中主要有三种播放模式:low latency playback.deep buffer playback和compressed offload playb ...

  5. Low Latency HLS的实现优化

    HTTP Live Streaming(HLS)是Apple公司主导提出并实现的基于HTTP的自适应码率流媒体通信协议(RFC8216). 说实在的,作为监控领域的玩家,我一直很抗拒Apple HLS ...

  6. low latency playback、deep buffer playback、compressed offload playback的区别 学习学习

    low latency playback.deep buffer playback.compressed offload playback的区别 https://blog.csdn.net/u0101 ...

  7. 【Skywalking】集成Sl4fj2报错:NoClassDefFoundError,com/lmax/disruptor/TimeoutBlockingWaitStrategy

    一.问题描述 jar:file:/opt/www/project/apache-skywalking/agent/activations/apm-toolkit-trace-activation-8. ...

  8. 使用LMAX Disruptor构建快速、线程安全的热点跟踪库

    LMAX Disruptor 是 Java 中最好的库之一,用于构建具有无锁队列的有界队列.Hubspot 撰写了有关 LMAX Disruptor 如何帮助构建快速.线程安全的跟踪库的文章: Hub ...

  9. LMAX Disruptor用户手册-4.0.0.RC2-最好的入门文章

    LMAX Disruptor 用户手册 原文链接 LMAX Disruptor是一个高性能线程通信库.它起源于LMAX对高并发,高性能,无锁算法的研究,如今已成长为Exchange基础架构的核心部分. ...

最新文章

  1. 离线安装Ubuntu虚拟机和GNURadio
  2. 系列笔记 | 深度学习连载(6):卷积神经网络基础
  3. android申请权限一次性申请多个,android 6.0以上动态一次申请多个权限-最美解决方案...
  4. 解决XShell连接时无法root用户登录问题【Linux】
  5. Chorme控制台console的用法;
  6. 二、Linxu的目录结构
  7. 移动开发在路上-- IOS移动开发系列 多线程三
  8. hdu2026.java字符
  9. c++ createtoolhelp32snapshot取进程路径_理解进程和线程
  10. Http之客户端请求服务器,服务器响应客户端。通过Handler在主线程中读取数据
  11. html可视化编辑器 mac,Sparkle For Mac v2.8.11 强大的可视化网页编辑设计工具 _ 黑苹果乐园...
  12. UNIAPP中IOS和安卓应用热更新和整包更新app的方法
  13. 【第17天】SQL进阶-查询优化- SHOW STATUS(SQL 小虚竹)
  14. android qq聊天动态表情的实现
  15. python appium连接安卓真机测试
  16. No1.初来乍到,请多指教
  17. 【机器学习】生成模型与判别模型详解
  18. SAP-PP 主生产计划MPS
  19. 短信管理器android,短信夹管理软件-短信夹管理app预约v1.4.3 安卓版-西西软件园...
  20. Linux下的Web开发工具(一)

热门文章

  1. 表格某一列不固定其余全固定_如何利用Python一键拆分表格并进行邮件发送~
  2. java 外部覆盖内部配置,Spring 与自定义注解、外部配置化的结合使用
  3. Qt中线程的生命期问题
  4. java平台设计zhe_基于java平台的网上评教系统的设计与实现
  5. kib,mib和mb,kb的区别
  6. 为安卓应用添加手势密码功能,遇到的一些问题以及解决方法
  7. fiddler实现模拟器抓吧_使用Fiddler对手机抓包
  8. mongodb 安装与卸载
  9. 知识图谱学习笔记-风控知识图谱设计
  10. 判断一颗二叉树是否为搜索二叉树和完全二叉树