コード例 #1
0
  /**
   * It is ok to have another thread rerun this method after a halt().
   *
   * @throws IllegalStateException if this object instance is already running in a thread
   */
  @Override
  public void run() {
    if (!running.compareAndSet(false, true)) {
      throw new IllegalStateException("Thread is already running");
    }
    sequenceBarrier.clearAlert();

    notifyStart();

    T event = null;
    long nextSequence = sequence.get() + 1L;
    while (true) {
      try {
        final long availableSequence = sequenceBarrier.waitFor(nextSequence);

        while (nextSequence <= availableSequence) {
          event = dataProvider.get(nextSequence);
          eventHandler.onEvent(event, nextSequence, nextSequence == availableSequence);
          nextSequence++;
        }

        sequence.set(availableSequence);
      } catch (final TimeoutException e) {
        notifyTimeout(sequence.get());
      } catch (final AlertException ex) {
        if (!running.get()) {
          break;
        }
      } catch (final Throwable ex) {
        exceptionHandler.handleEventException(ex, nextSequence, event);
        sequence.set(nextSequence);
        nextSequence++;
      }
    }

    notifyShutdown();

    running.set(false);
  }