disruptor主要有以下两种消费模式:

两种消费模式的共同点:消费线程的总数量是一样的,都等于传入的hander的数量,也就是入参多少handler处理器就有多少个线程被创建出来,代码参见:

    //重复消费的处理器代码:private void processEvents(){T event = null;long nextSequence = sequence.get() + 1L;while (true){try{final long availableSequence = sequenceBarrier.waitFor(nextSequence);if (batchStartAware != null){batchStartAware.onBatchStart(availableSequence - nextSequence + 1);}while (nextSequence <= availableSequence){event = dataProvider.get(nextSequence);eventHandler.onEvent(event, nextSequence, nextSequence == availableSequence);nextSequence++;}sequence.set(availableSequence);}catch (final TimeoutException e){notifyTimeout(sequence.get());}catch (final AlertException ex){if (running.get() != RUNNING){break;}}catch (final Throwable ex){exceptionHandler.handleEventException(ex, nextSequence, event);sequence.set(nextSequence);nextSequence++;}}}//不重复消费的代码public void run(){if (!running.compareAndSet(false, true)){throw new IllegalStateException("Thread is already running");}sequenceBarrier.clearAlert();notifyStart();boolean processedSequence = true;long cachedAvailableSequence = Long.MIN_VALUE;long nextSequence = sequence.get();T event = null;while (true){try{// if previous sequence was processed - fetch the next sequence and set// that we have successfully processed the previous sequence// typically, this will be true// this prevents the sequence getting too far forward if an exception// is thrown from the WorkHandlerif (processedSequence){processedSequence = false;do{nextSequence = workSequence.get() + 1L;sequence.set(nextSequence - 1L);}while (!workSequence.compareAndSet(nextSequence - 1L, nextSequence));}if (cachedAvailableSequence >= nextSequence){event = ringBuffer.get(nextSequence);workHandler.onEvent(event);processedSequence = true;}else{cachedAvailableSequence = sequenceBarrier.waitFor(nextSequence);}}catch (final TimeoutException e){notifyTimeout(sequence.get());}catch (final AlertException ex){if (!running.get()){break;}}catch (final Throwable ex){// handle, mark as processed, unless the exception handler threw an exceptionexceptionHandler.handleEventException(ex, nextSequence, event);processedSequence = true;}}notifyShutdown();running.set(false);}

不同点:
1.handler处理器的实现类不一样: 多线程重复消费的处理器实现的是EventHandler接口,多线程不重复消费的处理器实现的是WorkHandler接口
2.多线程重复消费的handler实现的关键在于每个handler(BatchEventProcessor)都维护一个sequence,互不干扰,重复消费
多线程不重复消费的handler实现的关键在于所有的handler(workerProcessor)都同享同一个workSequence,所以不会重复消费.

disruptor小结--消费者相关推荐

  1. disruptor小结--生产者代码

    disruptor的生产者有两种模式: 分别是单生产者模式和多生产者模式 1.单生产者模式: public long next(int n) { if (n < 1) { throw new I ...

  2. disruptor小结1--优势

    disruptor是一个内存队列,目的是为了线程间队列通信使用的,不同于kafka等分布式队列,区别于ArrayBlockQueue等队列的性能优势在于以下几点: a.环形队列(长度是2的n次幂) b ...

  3. disruptor消费者模型

    Disruptor的消费者实现是WorkerProcessor类,都实现了EventProcessor接口,这个接口继承了Runnable接口,因此,每个消费者实则就是一条线程,具体的业务逻辑都实现自 ...

  4. 生产者消费者模式-java原生、Disruptor实现方案

    生产者消费者模式介绍 生产者消费者模式是通过一个容器来解决生产者和消费者的强耦合问题.生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给阻 ...

  5. JAVA并发编程 之 LMAX Disruptor使用实例(高效解决生产者与消费者问题)

    什么是Disruptor? Disruptor是一个开源的JAVA框架,它被设计用于在生产者-消费者(producer-consumer problem,简称PCP)问题上获得尽量高的吞吐量(TPS) ...

  6. Disruptor框架中生产者、消费者的各种复杂依赖场景下的使用总结-我见过最好的Disruptor

    更多高并发知识请访问 www.itkc8.com 非常感谢 https://www.cnblogs.com/pku-liuqiang/p/8544700.html Disruptor是一个优秀的并发框 ...

  7. Disruptor并发框架--学习笔记

    Disruptor并发框架简介 Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在J ...

  8. Java 并发框架Disruptor(七)

    Disruptor VS BlockingQueue的压测对比: import java.util.concurrent.ArrayBlockingQueue;public class ArrayBl ...

  9. disruptor模拟高速处理大规模订单类业务场景

    什么是Disruptor? Disruptor是一个开源的JAVA框架,它被设计用于在生产者-消费者(producer-consumer problem,简称PCP)问题上获得尽量高的吞吐量(TPS) ...

最新文章

  1. springboot学习笔记(八)
  2. sqlplus -prelim,sqplus区别
  3. php总是报错,php - 简单工厂模式中的问题,总是报错
  4. Python 中的numpy 库
  5. hbase-1.3.2安装
  6. Python爬虫_HTTP标准
  7. 统一变更域本地管理员密码
  8. VirtualBox 虚拟机迁移-克隆
  9. scp远程传输文件之权限被拒绝解决方案
  10. Boundary loss for highly unbalanced segmentation
  11. 深度学习应用于脑电信号处理
  12. 消防应急疏散指示系统如何在工业厂房项目上应用
  13. 通过javaMail发送邮件,可选添加多个收件人,密送,抄送,多个附件,超实用
  14. 基址比例变址寻址(Base Index Scale Addressing)
  15. ESLint配置详解
  16. 如何把一张图片放到网页上或者如何在浏览器查看一张图片
  17. 后羿采集器怎么导出数据_后羿采集器如何采集历史天气数据这类直接就是表格的数据?...
  18. 百奥赛图与TRACON共同宣布YH001(CTLA-4单抗)联合恩沃利单抗(PD-L1)一线治疗软组织肉瘤的临床试验申请获得FDA批准
  19. 以色列AI营养数据公司获800万美元A轮融资
  20. java多线程模拟购买火车票

热门文章

  1. 微信支付将推双面屏;库克谈收购英特尔基带业务;Chrome 76 稳定版发布 | 极客头条...
  2. 任正非回应“华为营收下降300亿美元”;今日头条澄清「通讯录不属于用户隐私」;Kotlin 1.3.40发布 | 极客头条...
  3. 程序员 520 脱单指南!
  4. 抛弃 VS Code 我还能用啥编辑器?| 技术头条
  5. 阿里腾讯决战ToB“操作系统”
  6. 今日头条把微信按在地上摩擦
  7. 程序员如何应对双十一购物的大流量冲击?
  8. STM32学习:外部中断
  9. java web 中的乱码
  10. python库管理_Python中管理数据库