从零开始实现lmax-Disruptor队列(二)多消费者、消费者组间消费依赖原理解析

Java67

在v1版本的MyDisruptor实现单生产者、单消费者功能后。按照计划,v2版本的MyDisruptor需要支持多消费者和允许设置消费者组间的依赖关系。

由于这篇文章是一系列博客的一部分,我们需要了解前一篇博客的内容,以便更好地理解这篇博客。

[En]

Since the article is part of a series of blogs, we need to know the content of the previous blog in order to better understand this blog.

  • disruptor中的生产者和消费者是互相制约的,生产者的生产速度不能过快,在逻辑上队列已满时需要阻塞等待消费者进行消费,直到队列不满。
  • 为了支持多个消费者,上述描述需要调整:即生产商的生产速度不能太快。从逻辑上讲,当队列已满时,您需要阻塞并等待“最慢的消费者”完成消费,直到队列不满意为止。
    [En]

    to support multiple consumers, the above description needs to be adjusted: that is, the production speed of the producer cannot be too fast. Logically, when the queue is full, you need to block and wait for the " * slowest consumer * " to complete the consumption until the queue is dissatisfied.*

  • disruptor中每个消费者都拥有自己的消费序列号,生产者在生产时需要保证生产的序列号不能覆盖任何一个消费者,即生产者的序列号不能超过最慢的消费者序列号一圈(Producer.Sequence - SlowestConsumer.Sequence

```java
package mydisruptor;

import mydisruptor.util.SequenceUtil;
import mydisruptor.waitstrategy.MyWaitStrategy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**

输入验证码查看隐藏内容

扫描二维码关注本站微信公众号 Johngo学长
或者在微信里搜索 Johngo学长
回复 svip 获取验证码
wechat Johngo学长