disruptor是lmax公司开发的一款java高性能并发框架,其本质是一种类似队列的实现“生产者—消费者 ”模式的组件。

下面是其示例代码:

public class DisruptorServer {
private Disruptor disruptor = null; public static void main(String[] args) {
DisruptorContext.start();
System.out.println("Disruptor服务已启动..."); for(long i=0; i<101; i++){
DisruptorContext.publish(i);
}
DisruptorContext.stop();
System.out.println("...Disruptor服务已停止");
}
} public class DisruptorContext {
private static Disruptor<LongEvent> disruptor = null;
private static ExecutorService executor = null; public static void start(){
if(null==disruptor){
EventFactory<LongEvent> eventFactory = new LongEventFactory();
executor = Executors.newSingleThreadExecutor();
WaitStrategy waitStrategy = new BlockingWaitStrategy();
int ringBufferSize = 1024*1024;
disruptor = new Disruptor<LongEvent>(eventFactory,
ringBufferSize,
executor,
ProducerType.SINGLE,
waitStrategy);
EventHandler<LongEvent> eventHandler = new LongEventHandler();
disruptor.handleEventsWith(eventHandler);
disruptor.start();
}
} public static void stop(){
disruptor.shutdown();
executor.shutdown();
} public static void publish(long eventData){
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
long sequence = ringBuffer.next();
try{
LongEvent event = ringBuffer.get(sequence);
event.set(eventData);
}finally{
ringBuffer.publish(sequence);
}
}
} public class LongEvent {
private long value; public void set(long value) {
this.value = value;
} public long get(){
return this.value;
}
} public class LongEventFactory implements EventFactory<LongEvent> { @Override
public LongEvent newInstance() {
return new LongEvent();
} } public class LongEventHandler implements EventHandler<LongEvent>{ @Override
public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception {
System.out.println("Disruptor消费者输出Event :" + event.get());
} }

从构造函数来看一下disruptor的几个组成部分:

Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(eventFactory, ringBufferSize, executor, ProducerType.SINGLE, waitStrategy);

Event 需要进入disruptor交换的对象都需要封装成event,本例中封装的是一个long

EventFactory   工厂,决定以何种方式创建event。  
  *   工厂模式:我不知道你需要的是什么样的对象,索性把你的构造方法(工厂)传过来吧。

EventHandler  事件处理的具体实现,也即producer——consumer中的consumer的具体实现
  *   本例中仅仅对event中的long进行输出

ringBuffer  存放event的地方, 生产者和消费者之间的缓冲,相当于一个无锁的环形队列

executor    jdk5 java.concurrent.*包里的线程池 ,用来执行消费者线程

ProducerType.SINGLE  说明这个disruptor是单生产者模式的(disruptor也支持多生产者模式)

waitStrategy   消费者的等待策略, 有blocking、sleeping、yielding三种

关于多个消费者

ExecutorService executor executor = Executors.newCachedThreadPool(); WaitStrategy yieldStrategy = new YieldingWaitStrategy(); Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(eventFactory,                                              ringBufferSize,                                              executor,                                              ProducerType.SINGLE,                                              yieldStrategy);                                              如上这种情况下,在EventHandler中让每个event处理sleep 1秒,设置2个EventHandler(2个消费者),最后执行disruptor 向其publish 100个event,最后执行了大约100秒。

可见,消费者与消费者之间是并行处理的,但单个消费者内部的100个事件来说是以同步方式处理的。

Disruptor使用简介的更多相关文章

  1. Disruptor 使用简介

    [开发总结]Disruptor 使用简介 在极客时间看到王宝令老师关于 Disruptor 的一篇文章,觉得很有意思.看完之后又在网上找到一些其他关于Disruptor 的资料看了一下. 现在写篇文章 ...

  2. Java深入学习(6):Disruptor

    Disruptor框架简介: 并发框架,基于事件驱动,使用观察者模式 底层采用环形数组,取模算法 简单使用: /** * 声明一个Event:表示生产者和消费者之间传递的数据类型 */ public ...

  3. Disruptor并发框架(一)简介&上手demo

    框架简介 Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一 ...

  4. Disruptor并发框架简介

    Martin Fowler在自己网站上写一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金额交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平台上,其核心是一个业务逻辑处 ...

  5. disruptor 高并发编程 简介demo

    原文地址:http://www.cnblogs.com/qiaoyihang/p/6479994.html disruptor适用于大规模低延迟的并发场景.可用于读写操作分离.数据缓存,速度匹配(因为 ...

  6. 从构建分布式秒杀系统聊聊Disruptor高性能队列

    前言 秒杀架构持续优化中,基于自身认知不足之处在所难免,也请大家指正,共同进步.文章标题来自码友 简介 LMAX Disruptor是一个高性能的线程间消息库.它源于LMAX对并发性,性能和非阻塞算法 ...

  7. 无锁并发框架Disruptor学习入门

    刚刚听说disruptor,大概理一下,只为方便自己理解,文末是一些自己认为比较好的博文,如果有需要的同学可以参考. 本文目标:快速了解Disruptor是什么,主要概念,怎么用 1.Disrupto ...

  8. 01-项目简介Springboot简介入门配置项目准备

    总体课程主要分为4个阶段课程: ------------------------课程介绍------------------------ 01-项目简介Springboot简介入门配置项目准备02-M ...

  9. Disruptor 详解 一

    这篇博客将主要通过几个示例,简单讲述 Disruptor 的使用方法: 一.disruptor 简介 Disruptor 是英国外汇交易公司 LMAX 开发的一个无锁高性能的线程间消息传递的框架.目前 ...

随机推荐

  1. Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)

    1. 步骤一:恢复转账开发环境(转账开发环境见“https://www.cnblogs.com/wyhluckdog/p/10137283.html”) 2.步骤二:引入AOP的开发包3.步骤三:引入 ...

  2. Nginx+Keepalived实现站点高可用[z]

    http://segmentfault.com/a/1190000002881132

  3. Tornado异步阻塞解决方案

    在 tornado 中异步无阻塞的执行耗时任务 在 linux 上 tornado 是基于 epoll 的事件驱动框架,在网络事件上是无阻塞的.但是因为 tornado 自身是单线程的,所以如果我们在 ...

  4. Java 的CardPanel用法

    Java code? 1 2 3 4 5 6               card = new CardLayout(5,5);//5,5是组件间隔                pane = new ...

  5. LINUX 笔记5

    配置环境变量:(只是在当前命令行窗口中有用) 单独查看PATH环境变量,可用: [root@localhost u-boot-sh4]#echo $PATH 添加PATH环境变量,可用: [root@ ...

  6. 原生js:click和onclick本质的区别

    原生javascript的click在w3c里边的阐述是DOM button对象,也是html DOM click() 方法,可模拟在按钮上的一次鼠标单击. button 对象代表 HTML 文档中的 ...

  7. 2018.08.17 洛谷P3110 [USACO14DEC]驮运(最短路)

    传送门 一道sb最短路,从两个起点和终点跑一边最短路之后直接枚举两人的汇合点求最小值就行了. 代码: #include<bits/stdc++.h> #define N 40005 #de ...

  8. python操作数据库-安装

    首先是下载软件: 链接:http://pan.baidu.com/s/1nvp1imX 密码:6i0x 之后就是一系列设置. 安装教程:自行百度就行.需要注意的是设置my.ini时,需要加上这些东西( ...

  9. UVaLive 2796 Concert Hall Scheduling (最小费用流)

    题意:个著名的音乐厅因为财务状况恶化快要破产,你临危受命,试图通过管理的手段来拯救它,方法之一就是优化演出安排,既聪明的决定接受或拒绝哪些乐团的演出申请,使得音乐厅的收益最大化.该音乐厅有两个完全相同 ...

  10. MemCahced 使用及常见问题说明

    前言 本文档是针对Memcached使用及常见问题的说明. 一.获取 1. MemCached 官网:http://www.memcached.org 下载:http://memcached.org/ ...