package com.example.demo.producer;
import com.example.demo.consumer.LongEventHandler;
import com.example.demo.consumer.LongEventHandler2;
import com.example.demo.entity.LongEvent;
import com.example.demo.factory.LongEventFactory;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) {
// 1.创建一个可缓存的线程 提供线程来出发Consumer 的事件处理
ExecutorService executor = Executors.newCachedThreadPool();
// 2.创建工厂
EventFactory<LongEvent> eventFactory = new LongEventFactory();
// 3.创建ringBuffer 大小
// ringBufferSize大小一定要是2的N次方
int ringbuffer = 1024*1024;
// 4.创建Disruptor
Disruptor<LongEvent> disruptor = new Disruptor(eventFactory, ringbuffer, executor, ProducerType.MULTI, new YieldingWaitStrategy());
// 5.连接消费端方法
disruptor.handleEventsWith(new EventHandler[]{new LongEventHandler()});
disruptor.handleEventsWith(new EventHandler[]{new LongEventHandler2()});
// 6.启动
disruptor.start();
// 7.创建RingBuffer容器
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
// 8.创建生产者
LongEventProducer producer = new LongEventProducer(ringBuffer);
// 9.指定缓冲区大小
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
for(int i = 1; i < 100; ++i) {
byteBuffer.putLong(0, (long)i);
producer.onData(byteBuffer);
}
//10.关闭disruptor和executor
executor.shutdown();
disruptor.shutdown();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Disruptor专题简单案例资料
共16个文件
java:6个
class:6个
xml:4个
需积分: 3 0 下载量 137 浏览量
2023-06-17
22:25:54
上传
评论
收藏 16KB ZIP 举报
温馨提示
Disruptor专题简单案例资料 https://phoenix.blog.csdn.net/article/details/131264151
资源推荐
资源详情
资源评论
收起资源包目录
Disruptor.zip (16个子文件)
Disruptor
pom.xml 630B
src
test
java
main
resources
java
com
example
demo
producer
LongEventProducer.java 938B
Main.java 2KB
factory
LongEventFactory.java 314B
consumer
LongEventHandler2.java 432B
LongEventHandler.java 432B
entity
LongEvent.java 265B
.idea
workspace.xml 4KB
misc.xml 526B
compiler.xml 542B
target
classes
com
example
demo
producer
Main.class 2KB
LongEventProducer.class 2KB
factory
LongEventFactory.class 733B
consumer
LongEventHandler.class 1KB
LongEventHandler2.class 1KB
entity
LongEvent.class 551B
generated-sources
annotations
共 16 条
- 1
资源评论
不死鸟.亚历山大.狼崽子
- 粉丝: 5824
- 资源: 34
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功