disruptor的生产者有两种模式: 分别是单生产者模式和多生产者模式
1.单生产者模式:
public long next(int n)
{
if (n < 1)
{
throw new IllegalArgumentException(“n must be > 0”);
}
long nextValue = this.nextValue;
long nextSequence = nextValue + n;
long wrapPoint = nextSequence - bufferSize;
long cachedGatingSequence = this.cachedValue;
if (wrapPoint > cachedGatingSequence || cachedGatingSequence > nextValue)
{
cursor.setVolatile(nextValue); // StoreLoad fence
long minSequence;
while (wrapPoint > (minSequence = Util.getMinimumSequence(gatingSequences, nextValue)))
{
LockSupport.parkNanos(1L); // TODO: Use waitStrategy to spin?
}
this.cachedValue = minSequence;
}
this.nextValue = nextSequence;
return nextSequence;
}
这里核心的逻辑是队列满了之后的处理方式,可以看出来单生产者模式是使用LockSupport.parkNanos(1L);线程阻塞等待的方式
2.多生产者模式
```java
public long next(int n)
{
if (n < 1)
{
throw new IllegalArgumentException("n must be > 0");
}
long current;
long next;
do
{
current = cursor.get();
next = current + n;
long wrapPoint = next - bufferSize;
long cachedGatingSequence = gatingSequenceCache.get();
if (wrapPoint > cachedGatingSequence || cachedGatingSequence > current)
{
long gatingSequence = Util.getMinimumSequence(gatingSequences, current);
if (wrapPoint > gatingSequence)
{
LockSupport.parkNanos(1); // TODO, should we spin based on the wait strategy?
continue;
}
gatingSequenceCache.set(gatingSequence);
}
else if (cursor.compareAndSet(current, next))
{
break;
}
}
while (true);
return next;
}
这里核心的逻辑是队列满了之后的处理方式,可以看出来多生产者模式也是使用LockSupport.parkNanos(1L);线程阻塞等待的方式
总结:生产者在队列慢了之后都是阻塞等待,单生产者模式和多生产者模式如下所示: