博客
关于我
无锁并发框架-Disruptor的使用(二)
阅读量:381 次
发布时间:2019-03-05

本文共 4719 字,大约阅读时间需要 15 分钟。

Disruptor?????????????????????

1. ?????????

1.1 ????

??????Disruptor?????????????????????

com.lmax
disruptor
3.2.1
org.project.lombok
lombok
1.18.8
true

1.2 ??Event

?com.zhz.disruptor.event???????LongEvent?

package com.zhz.disruptor.event;import lombok.Data;/** * @author : zhz * @date : Created in 2020/12/30 * @version: V1.0 * @slogan: ??????????????? * @description: ????event ??Disruptor?????????? */@Datapublic class LongEvent {    private Long value;}

1.3 ??EventFactory

?com.zhz.disruptor.event????????LongEventFactory?

package com.zhz.disruptor.event;import com.lmax.disruptor.EventFactory;/** * @author : zhz * @date : Created in 2020/12/30 * @version: V1.0 * @slogan: ??????????????? * @description: ?????? */public class LongEventFactory implements EventFactory
{ @Override public LongEvent newInstance() { return new LongEvent(); }}

1.4 ???????

?com.zhz.disruptor?????????LongEventHandler?

package com.zhz.disruptor;import com.lmax.disruptor.EventHandler;import com.zhz.disruptor.event.LongEvent;import lombok.extern.slf4j.Slf4j;/** * @author : zhz * @date : Created in 2020/12/30 * @version: V1.0 * @slogan: ??????????????? * @description: ????? */@Slf4jpublic class LongEventHandler implements EventHandler
{ private long serial = 0; public LongEventHandler(long serial) { this.serial = serial; } @Override public void onEvent(LongEvent event, long sequence, boolean endOfBatch) throws Exception { log.info("???-{}: {}", this.serial, event.getValue()); }}

1.5 ?????

?com.zhz.disruptor?????????LongEventProducer?

package com.zhz.disruptor;import com.lmax.disruptor.RingBuffer;import com.zhz.disruptor.event.LongEvent;import lombok.extern.slf4j.Slf4j;import java.nio.ByteBuffer;/** * @author : zhz * @date : Created in 2020/12/30 * @version: V1.0 * @slogan: ??????????????? * @description: ????? */@Slf4jpublic class LongEventProducer {    private final RingBuffer ringBuffer;    public LongEventProducer(RingBuffer ringBuffer) {        this.ringBuffer = ringBuffer;    }    public void onData(ByteBuffer byteBuffer) {        long sequence = ringBuffer.next();        Long data = null;        try {            LongEvent event = ringBuffer.get(sequence);            data = byteBuffer.getLong(0);            event.setValue(data);            try {                Thread.sleep(10);            } catch (InterruptedException e) {                e.printStackTrace();            }        } finally {            log.info("?????????");            ringBuffer.publish(sequence);        }    }}

1.6 ??Main??

?com.zhz.disruptor???????DisruptorMain?

package com.zhz.disruptor;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;import com.zhz.disruptor.event.LongEvent;import com.zhz.disruptor.event.LongEventFactory;import java.nio.ByteBuffer;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * @author : zhz * @date : Created in 2020/12/30 * @version: V1.0 * @slogan: ??????????????? * @description:  */public class DisruptorMain {    public static void main(String[] args) {        // ?????????????????Consumer?????        ExecutorService executor = Executors.newCachedThreadPool();        // ????        EventFactory
eventFactory = new LongEventFactory(); // ??ringBuffer???????2?N?? int ringBufferSize = 1024; // ??Disruptor Disruptor
disruptor = new Disruptor<>(eventFactory, ringBufferSize, executor, ProducerType.SINGLE, new YieldingWaitStrategy()); // ??????? disruptor.handleEventsWith(new LongEventHandler(1), new LongEventHandler(2)); // ?? disruptor.start(); // ??RingBuffer?? RingBuffer
ringBuffer = disruptor.getRingBuffer(); // ????? LongEventProducer producer = new LongEventProducer(ringBuffer); // ??????? ByteBuffer byteBuffer = ByteBuffer.allocate(8); for (int i = 1; i <= 10; i++) { byteBuffer.putLong(0, i); producer.onData(byteBuffer); } //??disruptor?executor disruptor.shutdown(); executor.shutdown(); }}

2. ???????

  • ???????????????????????
  • ???????Lombok?@Data????POJO????????
  • ????????????????????
  • ????????????????????????????????
  • ????????????????????????????
  • ????????????????????????????

3. ??

??????Disruptor????????????????????????????????????????????????????????????????????????????????????????????????

转载地址:http://fbbwz.baihongyu.com/

你可能感兴趣的文章
Nodejs中搭建一个静态Web服务器,通过读取文件获取响应类型
查看>>
Nodejs中的fs模块的使用
查看>>
NodeJS使用淘宝npm镜像站的各种姿势
查看>>
NodeJs入门知识
查看>>
nodejs包管理工具对比:npm、Yarn、cnpm、npx
查看>>
NodeJs单元测试之 API性能测试
查看>>
nodejs图片转换字节保存
查看>>
nodejs在Liunx上的部署生产方式-PM2
查看>>
nodejs基于art-template模板引擎生成
查看>>
nodejs字符与字节之间的转换
查看>>
NodeJs学习笔记001--npm换源
查看>>
NodeJs学习笔记002--npm常用命令详解
查看>>
nodejs学习笔记一——nodejs安装
查看>>
vue3+Element-plus icon图标无法显示的问题(已解决)
查看>>
NodeJS实现跨域的方法( 4种 )
查看>>
nodejs封装http请求
查看>>
nodejs常用组件
查看>>
nodejs开发公众号报错 40164,白名单配置找不到,竟然是这个原因
查看>>
Nodejs异步回调的处理方法总结
查看>>
NodeJS报错 Fatal error: ENOSPC: System limit for number of file watchers reached, watch ‘...path...‘
查看>>