初步使用disruptor
来源:互联网 发布:cos后期特效软件 编辑:程序博客网 时间:2024/06/10 16:18
disruptor 是一个高性能的异步处理框架,可以认为是一个快速的的jms实现,从开发模式上讲是属于反应器模式
disruptor 的内部有个ringbuffer 是个很好的环形队列需要重点理解其原理
实例
package com.zyc.disruptor;import java.nio.ByteBuffer;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import com.lmax.disruptor.EventFactory;import com.lmax.disruptor.EventHandler;import com.lmax.disruptor.EventTranslator;import com.lmax.disruptor.EventTranslatorOneArg;import com.lmax.disruptor.RingBuffer;import com.lmax.disruptor.YieldingWaitStrategy;import com.lmax.disruptor.dsl.Disruptor;import com.lmax.disruptor.dsl.ProducerType;public class DisruptorMain {private static final EventTranslatorOneArg<EventEntity, Long> TRANSLATOR = new EventTranslatorOneArg<EventEntity, Long>() {@Overridepublic void translateTo(EventEntity event, long sequeue, Long buffer) {event.setValue(buffer);}};@SuppressWarnings("unchecked")public static void main(String[] args) throws InterruptedException {ExecutorService executorService = Executors.newCachedThreadPool();int ringBufferSize = 1024 * 1024;//ringbuffer的大小,最好是2的n次幂Disruptor<EventEntity> disruptor = new Disruptor<EventEntity>(new EventFactory<EventEntity>() {@Overridepublic EventEntity newInstance() {// TODO Auto-generated method stubreturn new EventEntity();}}, ringBufferSize, executorService, ProducerType.SINGLE,new YieldingWaitStrategy());disruptor.handleEventsWith(new EventHandler<EventEntity>() {@Overridepublic void onEvent(EventEntity arg0, long arg1, boolean arg2)throws Exception {Thread.sleep(5000);System.out.println(arg0.getValue());}},new CoustomerEventHandler2());//配置多个消费方,用","分割disruptor.start();RingBuffer<EventEntity> ringBuffer = disruptor.getRingBuffer();ringBuffer.publishEvent(TRANSLATOR,(long) 132);System.out.println("affdssdf");//此行验证异步是否可用disruptor.shutdown();executorService.shutdown();System.out.println("结束流程");}}class CoustomerEventHandler2 implements EventHandler<EventEntity> {@Overridepublic void onEvent(EventEntity arg0, long arg1, boolean arg2) throws Exception {System.out.println("this is a CoustomerEventHandler2 "+arg0.getValue());}}
package com.zyc.disruptor;public class EventEntity { private long value; public EventEntity(){ } public EventEntity(long string) {this.value=string;}public long getValue() { return value; } public void setValue(long value) { this.value = value; } }
阅读全文
0 0
- 初步使用disruptor
- Disruptor使用笔记
- Disruptor使用入门
- disruptor使用示例
- Disruptor的使用
- 简单使用Disruptor
- HBase中Disruptor使用
- Disruptor使用入门
- Disruptor的使用
- Disruptor
- disruptor
- disruptor
- disruptor
- disruptor
- disruptor
- Disruptor
- Disruptor
- Disruptor
- Java虚拟机学习记录,数据区域
- Spring-Boot初学之配置文件application.properties(web性能)
- tensorflow-placeholder-占位符的使用
- 欢迎使用CSDN-markdown编辑器
- BZOJ 2039: [2009国家集训队]employ人员雇佣 最小割 二元组建图模型
- 初步使用disruptor
- HDU --- 4305 Lighting 【生成树计数 + 向量 】
- 前端省市区(县)三级联动下拉框
- Java 计算两个日期之间的天数
- String与StringBuffer的区别
- spring boot Websocket
- 补码
- 30多条mysql数据库优化方法,千万级数据库记录查询轻松解决
- Java正则表达式入门