无锁并发框架-Disruptor的使用(二)

文章目录

1、生产消费模型的应用

1.1、引入依赖

com.lmax

disruptor

3.2.1

org.projectlombok

lombok

1.18.8

true

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

1.2、定义Event

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

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

1.3、定义EventFactory

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

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

1.4、定义事件消费者

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

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

1.5、定义生产者

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 {

public final RingBuffer ringBuffer;

public LongEventProducer(RingBuffer ringBuffer) {

this.ringBuffer = ringBuffer;

}

public void onData(ByteBuffer byteBuffer) {

// 1.ringBuffer 事件队列 下一个槽

long sequence = ringBuffer.next();

Long data = null;

try {

//2.取出空的事件队列

LongEvent event = ringBuffer.get(sequence);

//3.获取事件队列传递的数据

data = byteBuffer.getLong(0);

event.setValue(data);

try {

Thread.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

} finally {

log.info(“生产者准备发送数据”);

//4.发布事件,

//注意,最后的 ringBuffer.publish 方法必须包含在 finally 中以确保必须得到调用;

// 如果某个请求的 sequence 未被提交,将会堵塞后续的发布操作或者其它的 producer。

ringBuffer.publish(sequence);

}

}

}

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

  • 30

  • 31

  • 32

  • 33

  • 34

  • 35

  • 36

  • 37

  • 38

  • 39

  • 40

  • 41

  • 42

  • 43

  • 44

  • 45

  • 46

  • 47

  • 48

1.6、定义Main入口

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) {

// 1.创建一个可缓存的线程 提供线程来出发Consumer 的事件处理

ExecutorService executor = Executors.newCachedThreadPool();

// 2.创建工厂

EventFactory eventFactory = new LongEventFactory();

// 3.创建ringBuffer 大小,ringBufferSize大小一定要是2的N次方

int ringBufferSize = 1024;

// 4.创建Disruptor

Disruptor disruptor = new Disruptor(eventFactory, ringBufferSize, executor, ProducerTyp
e.SINGLE, new YieldingWaitStrategy());

// 5.连接消费端方法

disruptor.handleEventsWith(new LongEventHandler(1),new LongEventHandler(2));

// 6.启动

disruptor.start();

// 7.创建RingBuffer容器

RingBuffer ringBuffer = disruptor.getRingBuffer();

// 8.创建生产者

LongEventProducer producer = new LongEventProducer(ringBuffer);

// 9.指定缓冲区大小

ByteBuffer byteBuffer = ByteBuffer.allocate(8);

for (int i = 1; i <= 10; i++) {

byteBuffer.putLong(0,i);

producer.onData(byteBuffer);

}

//10.关闭disruptor和executor

disruptor.shutdown();

executor.shutdown();

}

}

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

  • 30

  • 31

  • 32

  • 33

  • 34

  • 35

  • 36

  • 37

  • 38

  • 39

  • 40

  • 41

  • 42

  • 43

  • 44

  • 45

  • 46

  • 47

  • 48

  • 49

  • 50

  • 51

  • 点赞

  • 评论

  • 分享

  • 收藏

  • 打赏

  • 举报

  • 已关注

最后

小编这些年深知大多数初中级工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此我收集整理了一份《2024年Java全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你需要这些资料,⬅专栏获取
ox)

  • 分享
  • 收藏
  • 打赏
  • 举报
  • 已关注

最后

小编这些年深知大多数初中级工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此我收集整理了一份《2024年Java全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-L0GqyF8x-1719725834968)]

[外链图片转存中…(img-IcRXicNh-1719725834968)]

[外链图片转存中…(img-Gq0vwfWA-1719725834969)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你需要这些资料,⬅专栏获取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值