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

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

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??????????
*/
@Data
public 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: ?????
*/
@Slf4j
public 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: ?????
*/
@Slf4j
public 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/

你可能感兴趣的文章
NiuShop开源商城系统 SQL注入漏洞复现
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 模型中的偏差和公平性检测
查看>>
Vue3.0 性能提升主要是通过哪几方面体现的?
查看>>
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>
NLP三大特征抽取器:CNN、RNN与Transformer全面解析
查看>>
NLP学习笔记:使用 Python 进行NLTK
查看>>
NLP度量指标BELU真的完美么?
查看>>
NLP的不同研究领域和最新发展的概述
查看>>
NLP的神经网络训练的新模式
查看>>
NLP采用Bert进行简单文本情感分类
查看>>
NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
查看>>
NLP项目:维基百科文章爬虫和分类【02】 - 语料库转换管道
查看>>
NLP:使用 SciKit Learn 的文本矢量化方法
查看>>
nmap 使用方法详细介绍
查看>>
Nmap扫描教程之Nmap基础知识
查看>>