Disruptor - SequenceBarrier

  • SequenceBarrier:一个协调屏障,用来跟踪发布者(publisher)的游标(cursor)和事件处理者(EventProcessor)的***(sequence)。

 Disruptor - SequenceBarrier

 long waitFor(long sequence) throws AlertException, InterruptedException, TimeoutException;

 等待给定的***可用,用来消费。 

* @throws AlertException       if a status change has occurred for the Disruptor
* @throws InterruptedException if the thread needs awaking on a condition variable.
* @throws TimeoutException     if a timeout occurs while waiting for the supplied sequence.

 long getCursor();

获取当前能读取到的游标(cursor)值。


boolean isAlerted();

barrier当前的状态是否是警报(alert)状态。


void alert();

提醒EventProcessor,一个状态发生了变化,直到清除之前,一直处于这种状态下。


void clearAlert();

清除当前的警报状态。


void checkAlert() throws AlertException;

检查是否提出了警报,如果提出了,就抛出异常。

@throws AlertException if alert has been raised.

 

这个接口提供了几个未实现方法,接下来看下它的实现类 -  ProcessingSequenceBarrier

Disruptor - SequenceBarrier 

Disruptor - SequenceBarrier 

(上图是从它的构造器中截取的一部分)

可以看出如果dependentSequence的长度是0,就将cursorSequence指向它,即两者有着相同的引用。

否则,通过FixedSequenceGroup来创建它,即与cursorSequence之间,两者独立存在。

Disruptor - SequenceBarrier 

 然后重点看下如下几个实现方法:

Disruptor - SequenceBarrier

Disruptor - SequenceBarrier

Disruptor - SequenceBarrier 

关于Sequencer和WaitStrategy的部分之后的博客中会一一叙述。