Disruptor它是一个开源的并发框架
官方地址|:
https://github.com/LMAX-Exchange/disruptor/wiki/Getting-Started
https://github.com/LMAX-Exchange/disruptor
public class LongEvent {
private Long value;
public Long getValue() {
return value;
}
public void setValue(Long value) {
this.value = value;
}
}
import com.lmax.disruptor.EventFactory;
/**
*事件工厂
*/
public class LongEventFactory implements EventFactory<LongEvent>{
@Override
public LongEvent newInstance() {
return new LongEvent();
}
}
import com.lmax.disruptor.EventHandler;
/**
* 消费者/事件处理器
*/
public class LongEventHandler implements EventHandler{
@Override
public void onEvent(Object event, long sequence, boolean endOfBatch)
throws Exception {
System.out.println("Event:"+event+" sequence:"+sequence+" endOfBatch:"+endOfBatch);
}
}
import java.nio.ByteBuffer;
import com.lmax.disruptor.RingBuffer;
public class LongEventProducer {
private final RingBuffer<LongEvent> ringBuffer;
public LongEventProducer(RingBuffer<LongEvent> ringBuffer) {
this.ringBuffer = ringBuffer;
}
public void onData(ByteBuffer bb) {
// 下一个序列
long sequence = ringBuffer.next();
try {
//获取Disruptor中的条目用于序列
LongEvent event = ringBuffer.get(sequence);
//填充数据
event.setValue(bb.getLong(0));
} finally {
//发布序列
ringBuffer.publish(sequence);
}
}
}
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 static final EventTranslatorOneArg<LongEvent, ByteBuffer> TRANSLATOR = new EventTranslatorOneArg<LongEvent, ByteBuffer>() {
public void translateTo(LongEvent event, long sequence, ByteBuffer bb) {
event.setValue(bb.getLong(0));;
}
};
public void onData(ByteBuffer bb) {
ringBuffer.publishEvent(TRANSLATOR, bb);
}
}
import java.nio.ByteBuffer;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
public class LongEventMain {
public static void main(String[] args) throws Exception {
Executor executor = Executors.newCachedThreadPool();
// bufferSize必须是2的N次方
int bufferSize = 1024;
// 构造disruptor
Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new,bufferSize, executor);
// 链接处理器
disruptor.handleEventsWith((event, sequence, endOfBatch) ->
System.out.println("Event: " + event+" sequence:"+sequence+" endOfBatch:"+endOfBatch));
// 启动运行Disruptor
disruptor.start();
//从disruptor中获取环形缓冲区
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
ByteBuffer bb = ByteBuffer.allocate(8);
for (long l = 0; true; l++) {
bb.putLong(0, l);
ringBuffer.publishEvent((event, sequence, buffer) -> event.setValue(buffer.getLong(0)),bb);
Thread.sleep(1000);
}
}
}
分享到:
相关推荐
Disruptor它是一个开源的并发框架,并获得2011 Duke’s 程序框架创新奖,能够在无锁的情况下实现网 络的Queue并发操作。本文是Disruptor官网中发布的文章的译文(现在被移到了GitHub)。
Netty整合并发编程框架Disruptor实战百万长链接服务构建源码.zip Netty整合并发编程框架Disruptor实战百万长链接服务构建源码.zip Netty整合并发编程框架Disruptor实战百万长链接服务构建源码.zip
Disruptor 是一个 Java 的并发编程框架,大大的简化了并发程序开发的难度,在性能上也比 Java 本身提供的一些并发包要好。 标签:Disruptor
资源内容:高并发框架Disruptor版本3.3.2,以及框架API使用及测试代码;
Java并发框架Disruptor,里面采取环形缓存结构,速度更快,适用于生产者消费者模式
springboot整合Disruptor并发编程框架,适合高并发场景需求的开发者
Disruptor它是一个开源的并发框架能够在无锁的情况下实现网络的Queue并发操作。同时,Disruptor是一个高性能的异步处理框架,或者可以认为是最快的消息框架(轻量的JMS),也可以认为是一个观察者模式的实现,或者...
在官方原有基础上为降低cpu占用修改版本,性能方面没有任何影响
Disruptor它是一个开源的并发框架,并获得2011 Duke’s 程序框架创新奖,能够在无锁的情况下实现网络的Queue并发操作。 Disruptor是一个高性能的异步处理框架,或者可以认为是最快的消息框架(轻量的JMS),也可以...
LMAX是一种新型零售金融交 易平台,它能够以很低的延迟产生大量交易。这个系统是建立在JVM平台上,其核心是一个业务逻辑处理 器,它能够在一个线程里每秒处理6百万订单。...业务逻辑处理器的核心是Disruptor。
最快的java并发框架disruptor已经将编译好的jar包集成在里面过阵子写个完整版本的示例再上传,本人其他资源也不错请大家多多下载
不错的框架,可以好好研究研究,速度下载,速度下载速度下载速度下载
NULL 博文链接:https://assertmyself.iteye.com/blog/2343301
其实Disruptor与其说是一个框架,不如说是一种设计思路,这个设计思路对于存在“并发、缓冲区、生产者—消费者模型、事务处理”这些元素的程序来说,Disruptor提出了一种大幅提升性能(TPS)的方案。
disruptor-3.4.2.jar 工具jar包 及 disruptor-3.4.2-sources.jar, Disruptor它是一个开源的并发框架,并获得2011 Duke’s 程序框架创新奖,能够在无锁的情况下实现网络的Queue并发操作,是 log4j2 引用的 jar 包
Okra是一个简单的使用JAVA开发的高性能,高扩展,高并发,低延迟的服务器框架。 主要目的是帮助中小团队快速开发实现网络游戏服务端。 本项目包含完整的MMORPG等游戏服务器的DEMO. Dependencies: JDK 1.8 : 框架...
disruptor-3.4.2.jar, 工具jar包, Disruptor它是一个开源的并发框架,并获得2011 Duke’s 程序框架创新奖,能够在无锁的情况下实现网络的Queue并发操作
并发框架Disruptor介绍Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易。这个系统是建立在JVM平台上,其核心是一个业务逻辑...
文档讲述了高并发框架disruptor的使用方法以及disruptor的原理以及为什么快的原因
Producer:生产者,泛指通过Disruptor发布事件的用户代码(实际业务代码,而并发框架代码)生成Event数据。 Event:事件,从生产者到消费者过程中的数据单元。由用户定义代码。 EventHandler:消费者,代表...