Disruptor源码介绍(一)-RingBuffer

RingBuffer是Disruptor最重要的核心组件,可以理解为一个环形队列,用来存储事件,生产者往队列上面存放事件,消费者去读取。内部是如何实现的,我们来看下源码。

1.类结构

/**
 * Ring based store of reusable entries containing the data representing
 * an event being exchanged between event producer and {@link EventProcessor}s.
 *
 * @param <E> implementation storing the data for sharing during exchange or parallel coordination of an event.
 */
public final class RingBuffer<E> extends RingBufferFields<E> implements Cursored, EventSequencer<E>, EventSink<E>
看类的注释,翻译成中文就是:基于环形结构存储可重用条目包含一些数据,这些数据可以表示在事件生产者和EventProcessor之间交换的事件
可以看到RingBuffer实现了一系列接口Cursored, EventSequencer<E>, EventSink<E>,继承了RingBufferFields<E>
我们来一个个看下接口:

1.1 Cursored

public interface Cursored
{
    /**
     * Get the current cursor value.
     *
     * @return current cursor value
     */
    long getCursor();
}

Cursored接口只提供了一个获取当前序列值(游标)的方法

1.2 EventSequencer

public interface EventSequencer<T> extends DataProvider<T>, Sequenced
{

}
看下DataProvider和Sequenced
1.2.1 DataProvider
public interface DataProvider<T>
{
    T get(long sequence);
}
提供了一个根据序列号获得数据的方法
1.2.2 Sequenced
Sequenced定义的方法就比较多了,主要是跟序列有关,例如ringBuffer的size,下个发布事件的序列号等等

所以EventSequencer扩展了Sequenced,提供了一些序列功能;同时扩展了DataProvider,提供了按序列值来获取数据的功能。

1.3 EventSink
public interface EventSink<E>
{
    /**
     * Publishes an event to the ring buffer.  It handles
     * claiming t
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值