一 概述

1.Disruptor

Disruptor是一个高性能的异步处理框架,一个“生产者-消费者”模型。

2.RingBuffer

RingBuffer是一种环形数据结构,包含一个指向下一个槽点的序号,可以在线程间传递数据。

3.Event

在Disruptor框架中,生产者生产的数据叫做Event。

二 Disruptor框架基本构成

1.MyEvent:自定义对象,充当“生产者-消费者”模型中的数据。
2.MyEventFactory:实现EventFactory的接口,用于生产数据。
3.MyEventProducerWithTranslator:将数据存储到自定义对象中并发布。
4.MyEventHandler:自定义消费者。

三 Demo

初次接触Disruptor,认识停留在表面,零散,模糊,在此记一个简单的示例,以便日后深入研究。

1.自定义数据类

package com.disruptor.basic;

public class LongEvent {
private long value; public long getValue() {
return value;
} public void setValue(long value) {
this.value = value;
} }

2.数据生产工厂(创建数据类对象)

package com.disruptor.basic;

import com.lmax.disruptor.EventFactory;

public class LongEventFactory implements EventFactory<LongEvent> {

    public LongEvent newInstance() {
// TODO Auto-generated method stub
return new LongEvent();
} }

3.数据源(初始化数据对象并发布)

package com.disruptor.basic;

import java.nio.ByteBuffer;

import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer; public class LongEventProducerWithTranslator { private final RingBuffer<LongEvent> ringBuffer; public LongEventProducerWithTranslator(RingBuffer<LongEvent> ringBuffer) {
this.ringBuffer = ringBuffer;
} private final EventTranslatorOneArg<LongEvent, ByteBuffer> TRANSLATOR = new EventTranslatorOneArg<LongEvent, ByteBuffer>() {
/**
* event:包含有消费数据的对象; sequence:分配给目标对象的RingBuffer空间序号;
* bb:包含有将要被存储到目标对象中的数据的容器
*/
public void translateTo(LongEvent event, long sequence, ByteBuffer bb) {
// TODO Auto-generated method stub
event.setValue(bb.getLong(0));// 将数据存储到目标对象中
}
}; public void onData(ByteBuffer bb) {
ringBuffer.publishEvent(TRANSLATOR, bb);// 发布,将数据推送给消费者
} }

4.消费者

package com.disruptor.basic;

import com.lmax.disruptor.EventHandler;

public class LongEventHandler implements EventHandler<LongEvent> {

    public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception {
// TODO Auto-generated method stub
System.out.println("当前消费的数据="+event.getValue());
} }

5.测试类

package com.disruptor.basic;

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import org.junit.Test; import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType; public class LongEventTest { @SuppressWarnings({ "unchecked", "deprecation" })
@Test
public void test01() throws InterruptedException {
ExecutorService executor = Executors.newCachedThreadPool();
EventFactory<LongEvent> factory = new LongEventFactory();
int bufferSize = 1024;
Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factory, bufferSize, executor, ProducerType.SINGLE,
new YieldingWaitStrategy());
disruptor.handleEventsWith(new LongEventHandler());
disruptor.start(); RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
// LongEventProducer producer = new
// LongEventProducer(ringBuffer);
LongEventProducerWithTranslator producer = new LongEventProducerWithTranslator(ringBuffer);
ByteBuffer bb = ByteBuffer.allocate(8);
// long startTime = System.currentTimeMillis();
for (long a = 0; a < 100; a++) {
bb.putLong(0, a);
producer.onData(bb);
/*if (a == 99) {
long endTime = System.currentTimeMillis();
System.out.println("useTime=" + (endTime - startTime));
}*/
Thread.sleep(100);
}
/*long endTime = System.currentTimeMillis();
System.out.println("useTime=" + (endTime - startTime));*/
disruptor.shutdown();
executor.shutdown();
} /*@Test
public void test02() {
long startTime = System.currentTimeMillis();
for (long a = 0; a < 100; a++) {
System.out.println(a);
}
long endTime = System.currentTimeMillis();
System.out.println("useTime=" + (endTime - startTime));
}*/ }

Disruptor之粗糙认识的更多相关文章

  1. 架构师养成记--15.Disruptor并发框架

    一.概述 disruptor对于处理并发任务很擅长,曾有人测过,一个线程里1s内可以处理六百万个订单,性能相当感人. 这个框架的结构大概是:数据生产端 --> 缓存 --> 消费端 缓存中 ...

  2. 并发框架Disruptor浅析

    1.引言 Disruptor是一个开源的Java框架,它被设计用于在生产者—消费者(producer-consumer problem,简称PCP)问题上获得尽量高的吞吐量(TPS)和尽量低的延迟.D ...

  3. LMAX Disruptor—多生产者多消费者中,消息复制分发的高性能实现

    解决的问题 当我们有多个消息的生产者线程,一个消费者线程时,他们之间如何进行高并发.线程安全的协调? 很简单,用一个队列. 当我们有多个消息的生产者线程,多个消费者线程,并且每一条消息需要被所有的消费 ...

  4. Disruptor 极速体验

    已经不记得最早接触到 Disruptor 是什么时候了,只记得发现它的时候它是以具有闪电般的速度被介绍的.于是在脑子里, Disruptor 和"闪电"一词关联了起来,然而却一直没 ...

  5. disruptor - Concurrent Programming Framework 并发编程框架

    disruptor发布了Java的2.0版本(.Net版本见这里),disruptor是一个高性能的异步处理框架,或者可以认为是最快的消息框架(轻量的JMS),也可以认为是一个观察者模式实现,或者事件 ...

  6. 剖析Disruptor:为什么会这么快?(二)神奇的缓存行填充

    原文链接:http://mechanitis.blogspot.com/2011/07/dissecting-disruptor-why-its-so-fast_22.html 需FQ 计算机入门   ...

  7. The LMAX disruptor Architecture--转载

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

  8. Disruptor 源码阅读笔记--转

    原文地址:http://coderbee.net/index.php/open-source/20130812/400 一.Disruptor 是什么? Disruptor 是一个高性能异步处理框架, ...

  9. 构建高性能服务(三)Java高性能缓冲设计 vs Disruptor vs LinkedBlockingQueue--转载

    原文地址:http://maoyidao.iteye.com/blog/1663193 一个仅仅部署在4台服务器上的服务,每秒向Database写入数据超过100万行数据,每分钟产生超过1G的数据.而 ...

随机推荐

  1. [AHOI2009]中国象棋 BZOJ1801 dp

    题目描述 这次小可可想解决的难题和中国象棋有关,在一个N行M列的棋盘上,让你放若干个炮(可以是0个),使得没有一个炮可以攻击到另一个炮,请问有多少种放置方法.大家肯定很清楚,在中国象棋中炮的行走方式是 ...

  2. springboot整合mybatis,redis,代码(一)

    一 搭建项目,代码工程结构 使用idea或者sts构建springboot项目 二  数据库sql语句 SQLyog Ultimate v12.08 (64 bit) MySQL - 5.7.14-l ...

  3. HTTP记录

    -------------TCP握手协议------------- 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. [第一次握手]建立连接时,客户端发送syn包(syn ...

  4. PHP 实时生成并下载超大数据量的 Excel 文件

    //另外由于excel数据是从数据库里逐步读出然后写入输出流的所以需要将PHP的执行时间设长一点 //(默认30秒)set_time_limit(0)不对PHP执行时间做限制. set_time_li ...

  5. docker load error: open /var/lib/docker/tmp/docker-import-347673752/bin/json: no such file or directory

    docker save 对应 docker load docker export 对应 docker import 在导出的包的环境中的docker版本跟需要导入的环境中的docker版本不一致也可能 ...

  6. 洛谷 P3239 [HNOI2015]亚瑟王(期望dp)

    题面 luogu 题解 一道复杂的期望\(dp\) 思路来源:__stdcall 容易想到,只要把每张牌打出的概率算出来就可以求出\(ans\) 设\(fp[i]\)表示把第\(i\)张牌打出来的概率 ...

  7. Codeforces - 617E 年轻人的第一道莫队·改

    题意:给出\(n,m,k,a[1...n]\),对于每次询问,求\([l,r]\)中\(a[i] \ xor \ a[i+1] \ xor \ ...a[j],l<=i<=j<=r\ ...

  8. CodeChef - NWAYS 组合数 朱世杰恒等式

    这道题目数据有坑,白浪费一个小时! 题意:求\(\sum_{i=1}^n\sum_{j=1}^n{|i-j|+k \choose k}\) 知识点: 朱世杰恒等式,\(\sum_{i=r}^n{i \ ...

  9. c# MVC返回小驼峰Json(首字母小写)

    1.与前端交互时,前端总希望传过去的json字段名首字母小写,但是.net规范是首字线大写 如果就写了下面的转换方法 /// <summary> /// Poco类字段名转换成首字母小写的 ...

  10. PIE SDK创建掩膜

      1.算法功能简介 图像掩膜(Mask)用选定的图像.图形或物体,对处理的图像(全部或局部)进行遮挡,来控制图像处理的区域或处理过程.掩膜是一种图像滤镜的模板,实用掩膜经常处理的是遥感图像.当提取道 ...