博客
关于我
无锁并发框架-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/

你可能感兴趣的文章
Nokia5233手机和我装的几个symbian V5手机软件
查看>>
non linear processor
查看>>
Non-final field ‘code‘ in enum StateEnum‘
查看>>
none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
查看>>
None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
查看>>
NoNodeAvailableException None of the configured nodes are available异常
查看>>
Vue.js 学习总结(16)—— 为什么 :deep、/deep/、>>> 样式能穿透到子组件
查看>>
nopcommerce商城系统--文档整理
查看>>
NOPI读取Excel
查看>>
NoSQL&MongoDB
查看>>
NoSQL介绍
查看>>
NoSQL数据库概述
查看>>
Notadd —— 基于 nest.js 的微服务开发框架
查看>>
NOTE:rfc5766-turn-server
查看>>
Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad++正则表达式替换字符串详解
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notes on Paul Irish's "Things I learned from the jQuery source" casts
查看>>