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

你可能感兴趣的文章
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
查看>>
nginx添加模块与https支持
查看>>
Nginx用户认证
查看>>
Nginx的Rewrite正则表达式,匹配非某单词
查看>>
Nginx的使用总结(一)
查看>>
Nginx的使用总结(二)
查看>>
Nginx的可视化神器nginx-gui的下载配置和使用
查看>>
Nginx的是什么?干什么用的?
查看>>
Nginx访问控制_登陆权限的控制(http_auth_basic_module)
查看>>
nginx负载均衡器处理session共享的几种方法(转)
查看>>
nginx负载均衡的5种策略(转载)
查看>>
nginx负载均衡的五种算法
查看>>
Nginx运维与实战(二)-Https配置
查看>>
Nginx配置ssl实现https
查看>>
Nginx配置TCP代理指南
查看>>
Nginx配置——不记录指定文件类型日志
查看>>
Nginx配置代理解决本地html进行ajax请求接口跨域问题
查看>>
Nginx配置参数中文说明
查看>>
Nginx配置好ssl,但$_SERVER[‘HTTPS‘]取不到值
查看>>
Nginx配置如何一键生成
查看>>