一 概述

1.Disruptor

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

2.RingBuffer

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

3.Event

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

二 Disruptor框架基本构成

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

三 Demo

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

1.自定义数据类

  1. package com.disruptor.basic;
  2.  
  3. public class LongEvent {
  4. private long value;
  5.  
  6. public long getValue() {
  7. return value;
  8. }
  9.  
  10. public void setValue(long value) {
  11. this.value = value;
  12. }
  13.  
  14. }

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

  1. package com.disruptor.basic;
  2.  
  3. import com.lmax.disruptor.EventFactory;
  4.  
  5. public class LongEventFactory implements EventFactory<LongEvent> {
  6.  
  7. public LongEvent newInstance() {
  8. // TODO Auto-generated method stub
  9. return new LongEvent();
  10. }
  11.  
  12. }

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

  1. package com.disruptor.basic;
  2.  
  3. import java.nio.ByteBuffer;
  4.  
  5. import com.lmax.disruptor.EventTranslatorOneArg;
  6. import com.lmax.disruptor.RingBuffer;
  7.  
  8. public class LongEventProducerWithTranslator {
  9.  
  10. private final RingBuffer<LongEvent> ringBuffer;
  11.  
  12. public LongEventProducerWithTranslator(RingBuffer<LongEvent> ringBuffer) {
  13. this.ringBuffer = ringBuffer;
  14. }
  15.  
  16. private final EventTranslatorOneArg<LongEvent, ByteBuffer> TRANSLATOR = new EventTranslatorOneArg<LongEvent, ByteBuffer>() {
  17. /**
  18. * event:包含有消费数据的对象; sequence:分配给目标对象的RingBuffer空间序号;
  19. * bb:包含有将要被存储到目标对象中的数据的容器
  20. */
  21. public void translateTo(LongEvent event, long sequence, ByteBuffer bb) {
  22. // TODO Auto-generated method stub
  23. event.setValue(bb.getLong(0));// 将数据存储到目标对象中
  24. }
  25. };
  26.  
  27. public void onData(ByteBuffer bb) {
  28. ringBuffer.publishEvent(TRANSLATOR, bb);// 发布,将数据推送给消费者
  29. }
  30.  
  31. }

4.消费者

  1. package com.disruptor.basic;
  2.  
  3. import com.lmax.disruptor.EventHandler;
  4.  
  5. public class LongEventHandler implements EventHandler<LongEvent> {
  6.  
  7. public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception {
  8. // TODO Auto-generated method stub
  9. System.out.println("当前消费的数据="+event.getValue());
  10. }
  11.  
  12. }

5.测试类

  1. package com.disruptor.basic;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.Executors;
  6.  
  7. import org.junit.Test;
  8.  
  9. import com.lmax.disruptor.EventFactory;
  10. import com.lmax.disruptor.RingBuffer;
  11. import com.lmax.disruptor.YieldingWaitStrategy;
  12. import com.lmax.disruptor.dsl.Disruptor;
  13. import com.lmax.disruptor.dsl.ProducerType;
  14.  
  15. public class LongEventTest {
  16.  
  17. @SuppressWarnings({ "unchecked", "deprecation" })
  18. @Test
  19. public void test01() throws InterruptedException {
  20. ExecutorService executor = Executors.newCachedThreadPool();
  21. EventFactory<LongEvent> factory = new LongEventFactory();
  22. int bufferSize = 1024;
  23. Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factory, bufferSize, executor, ProducerType.SINGLE,
  24. new YieldingWaitStrategy());
  25. disruptor.handleEventsWith(new LongEventHandler());
  26. disruptor.start();
  27.  
  28. RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
  29. // LongEventProducer producer = new
  30. // LongEventProducer(ringBuffer);
  31. LongEventProducerWithTranslator producer = new LongEventProducerWithTranslator(ringBuffer);
  32. ByteBuffer bb = ByteBuffer.allocate(8);
  33. // long startTime = System.currentTimeMillis();
  34. for (long a = 0; a < 100; a++) {
  35. bb.putLong(0, a);
  36. producer.onData(bb);
  37. /*if (a == 99) {
  38. long endTime = System.currentTimeMillis();
  39. System.out.println("useTime=" + (endTime - startTime));
  40. }*/
  41. Thread.sleep(100);
  42. }
  43. /*long endTime = System.currentTimeMillis();
  44. System.out.println("useTime=" + (endTime - startTime));*/
  45. disruptor.shutdown();
  46. executor.shutdown();
  47. }
  48.  
  49. /*@Test
  50. public void test02() {
  51. long startTime = System.currentTimeMillis();
  52. for (long a = 0; a < 100; a++) {
  53. System.out.println(a);
  54. }
  55. long endTime = System.currentTimeMillis();
  56. System.out.println("useTime=" + (endTime - startTime));
  57. }*/
  58.  
  59. }

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. 在Windows上搭建Git Server (2)

      第一步:下载Java,下载地址:http://www.java.com/zh_CN/ 第二步:安装Java.安装步骤不再详述. 第三步:配置Java环境变量. 右键”计算机” => ”属性” ...

  2. easyui datagrid可编辑表格使用经验分享

    文章目录 1相关接口方法 2列属性formatter 3编辑器类型 3.1基于my97的编辑器 3.2简单的密码编辑器 3.3动态增加/删除编辑器 4字段的级联操作 4.1combobox的级联操作 ...

  3. 对KMP算法通过代码生成next数组理解

    本文是根据考研数据结构2019版天勤高分笔记理解编写的: 首先给出代码: 1 void getnext(Str substr,int next[]){ 2 int i=0,j=0; 3 next[1] ...

  4. 老男孩python作业4-ATM程序开发

    实现一个ATM + 购物商城程序: 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录 支持账户间转账 记录每月日常消费流水 提供还款 ...

  5. LeetCode8. 字符串转整数 (atoi)

    8. 字符串转整数 (atoi) 描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连 ...

  6. 基于 bootstrap html 响应式 布局

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  7. C# 利用Powershell获取网络相关信息

    利用Get-NetAdapter获取信息 Get-NetAdapter 参考链接:https://docs.microsoft.com/en-us/powershell/module/netadapt ...

  8. phantomjs的和谷歌浏览器的简单使用

    一.phantomjs的简单使用 ''' 什么是phantomJs:无界面的浏览器 ''' from selenium import webdriver from time import sleep ...

  9. ssh无密码登录和scp无密码拷贝

    目的:在A主机上无密码登录B主机 方法: A主机生成密钥:ssh-keygen -t rsa 将密钥复制到B主机:cat ~/.ssh/id_rsa.pub | ssh root@B 'cat > ...

  10. css引入 以及选择器040

    css的介绍: css(Cascading Style Sheet)  层叠样式表 作用就是给HTML页面标签议案家各种样式 定义网页效果 简单来说 就是讲网页内容和显示样式进行分离 , 提高了显示功 ...