Java Code Examples for com.lmax.disruptor.Sequence#get()

The following examples show how to use com.lmax.disruptor.Sequence#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SleepWaitStrategyFactory.java    From fastjgame with Apache License 2.0 6 votes vote down vote up
@Override
public long waitFor(final long sequence, Sequence cursor, final Sequence dependentSequence,
                    final SequenceBarrier barrier) throws AlertException {
    long availableSequence;
    int counter = retries;

    // dependentSequence 该项目组织架构中,其实只是生产者的sequence,也就是cursor
    while ((availableSequence = dependentSequence.get()) < sequence) {
        counter = applyWaitMethod(barrier, counter);

        // 每睡眠一次,执行一次循环
        if (counter <= 0) {
            eventLoop.safeLoopOnce();
        }
    }
    return availableSequence;
}
 
Example 2
Source File: BusySpinWaitStrategyFactory.java    From fastjgame with Apache License 2.0 6 votes vote down vote up
@Override
public long waitFor(final long sequence, Sequence cursor, final Sequence dependentSequence,
                    final SequenceBarrier barrier) throws AlertException, InterruptedException {

    long availableSequence;
    int spinTries = 0;

    // dependentSequence 该项目组织架构中,其实只是生产者的sequence,也就是cursor
    while ((availableSequence = dependentSequence.get()) < sequence) {
        barrier.checkAlert();
        Thread.onSpinWait();

        if (++spinTries == loopOnceSpinTries) {
            spinTries = 0;
            eventLoop.safeLoopOnce();
        }
    }

    return availableSequence;
}
 
Example 3
Source File: SleepingBlockingWaitStrategy.java    From ballerina-message-broker with Apache License 2.0 6 votes vote down vote up
public long waitFor(long sequence, Sequence cursorSequence, Sequence dependentSequence, SequenceBarrier barrier)
        throws AlertException, InterruptedException {
    if (cursorSequence.get() < sequence) {
        this.lock.lock();

        try {
            while (cursorSequence.get() < sequence) {
                barrier.checkAlert();
                this.processorNotifyCondition.await();
            }
        } finally {
            this.lock.unlock();
        }
    }

    long availableSequence;
    while ((availableSequence = dependentSequence.get()) < sequence) {
        barrier.checkAlert();
        LockSupport.parkNanos(1L);
    }

    return availableSequence;
}
 
Example 4
Source File: ExponentionallyIncreasingSleepingWaitStrategy.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Override
public long waitFor(final long sequence, Sequence cursor, final Sequence dependentSequence, final SequenceBarrier barrier) throws AlertException {
    long availableSequence;
    int currentSleep = sleepTimeNsStart;

    while ((availableSequence = dependentSequence.get()) < sequence) {
        currentSleep = applyWaitMethod(barrier, currentSleep);
    }

    return availableSequence;
}
 
Example 5
Source File: LoggerServiceImpl.java    From gflogger with Apache License 2.0 5 votes vote down vote up
public long waitFor(
	long sequence,
	Sequence cursor,
	Sequence dependentSequence,
	SequenceBarrier barrier
) throws AlertException, InterruptedException, TimeoutException {
	long availableSequence;
	if ((availableSequence = cursor.get()) < sequence) {
		flush();
		synchronized (lock) {
			++numWaiters;
			while ((availableSequence = cursor.get()) < sequence) {
				if (state == State.STOPPED) {
					disruptor.halt();
					throw AlertException.INSTANCE;
				}
				barrier.checkAlert();
				//*/
				lock.wait();
				/*/
				Thread.sleep(1);
				//*/
			}
			--numWaiters;
		}
	}
	while ((availableSequence = dependentSequence.get()) < sequence) {
		barrier.checkAlert();
	}

	return availableSequence;
}
 
Example 6
Source File: ParkWaitStrategy.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
@Override
public long waitFor(long sequence,
                    Sequence cursor,
                    Sequence dependentSequence,
                    SequenceBarrier barrier) throws AlertException,
  InterruptedException,
  TimeoutException {
  long availableSequence;
  while ((availableSequence = dependentSequence.get()) < sequence) {
    barrier.checkAlert();
    LockSupport.parkNanos(parkFor);
  }
  return availableSequence;
}
 
Example 7
Source File: SpinLoopHintBusySpinWaitStrategy.java    From perf-workshop with Apache License 2.0 5 votes vote down vote up
@Override
public long waitFor(final long sequence, Sequence cursor, final Sequence dependentSequence, final SequenceBarrier barrier)
        throws AlertException, InterruptedException
{
    long availableSequence;

    while ((availableSequence = dependentSequence.get()) < sequence)
    {
        SpinHint.spinLoopHint();
        barrier.checkAlert();
    }

    return availableSequence;
}
 
Example 8
Source File: SamplingProfiler.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@Override
public long waitFor(long sequence, Sequence cursor, Sequence dependentSequence, SequenceBarrier barrier) {
    return dependentSequence.get();
}
 
Example 9
Source File: RingBufferSubscriberUtils.java    From camunda-bpm-reactor with Apache License 2.0 4 votes vote down vote up
public static <T> boolean waitRequestOrTerminalEvent(
  Sequence pendingRequest,
  RingBuffer<MutableSignal<T>> ringBuffer,
  SequenceBarrier barrier,
  Subscriber<? super T> subscriber,
  AtomicBoolean isRunning
) {
  final long waitedSequence = ringBuffer.getCursor() + 1L;
  try {
    MutableSignal<T> event = null;
    while (pendingRequest.get() < 0l) {
      //pause until first request
      if (event == null) {
        barrier.waitFor(waitedSequence);
        event = ringBuffer.get(waitedSequence);

        if (event.type == MutableSignal.Type.COMPLETE) {
          try {
            subscriber.onComplete();
            return false;
          } catch (Throwable t) {
            Exceptions.throwIfFatal(t);
            subscriber.onError(t);
            return false;
          }
        } else if (event.type == MutableSignal.Type.ERROR) {
          subscriber.onError(event.error);
          return false;
        }
      } else {
        barrier.checkAlert();
      }
      LockSupport.parkNanos(1l);
    }
  } catch (TimeoutException te) {
    //ignore
  } catch (AlertException ae) {
    if (!isRunning.get()) {
      return false;
    }
  } catch (InterruptedException ie) {
    Thread.currentThread().interrupt();
  }

  return true;
}