Java Code Examples for java.util.concurrent.CompletionStage#thenRun()

The following examples show how to use java.util.concurrent.CompletionStage#thenRun() . 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: AsyncIterators.java    From java-async-util with Apache License 2.0 6 votes vote down vote up
@Override
public CompletionStage<Either<End, U>> nextStage() {
  final CompletableFuture<Either<End, U>> listener = new CompletableFuture<>();
  final CompletionStage<Void> nextFinished = attachListener(listener);

  nextFinished.thenRun(() -> {
    AsyncTrampoline
        .asyncWhile(() -> StageSupport.tryComposeWith(this.lock.acquireLock(), token -> {
          if (this.closed) {
            return StageSupport.completedStage(false);
          }
          return fillMore()
              .thenApply(Either::isRight)
              // exceptional futures get added to the queue same as normal ones,
              // we may continue filling
              .exceptionally(e -> true);
        }));
  });

  return listener;
}
 
Example 2
Source File: MockChannel.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void registerEvent(RawEvent event) throws IOException {
    int ops = event.interestOps();
    if ((ops & SelectionKey.OP_WRITE) != 0) {
        synchronized (stateLock) {
            checkOpen();
            executor.execute(event::handle);
        }
    } else if ((ops & SelectionKey.OP_READ) != 0) {
        CompletionStage<?> cs;
        synchronized (readLock) {
            cs = currentRule().whenReady();
            synchronized (stateLock) {
                checkOpen();
                cs.thenRun(() -> executor.execute(event::handle));
            }
        }
    } else {
        throw new RuntimeException("Unexpected registration: " + ops);
    }
}
 
Example 3
Source File: CompletableToCompletionStageTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private void verifyComplete(boolean completeBeforeListen) throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    CompletionStage<Void> stage = source.toCompletionStage();
    if (completeBeforeListen) {
        source.onComplete();
        stage.thenRun(latch::countDown);
    } else {
        stage.thenRun(latch::countDown);
        source.onComplete();
    }
    latch.await();
}
 
Example 4
Source File: CompletedStage.java    From java-async-util with Apache License 2.0 5 votes vote down vote up
@Override
public CompletionStage<Void> runAfterBoth(final CompletionStage<?> other, final Runnable action) {
  Objects.requireNonNull(action);
  return other.thenRun(
      this.exception == null
          ? action
          : () -> {
            throw CompletedStage.wrapIfNecessary(this.exception);
          });
}
 
Example 5
Source File: AkkaApplication.java    From reactive-code-workshop with Apache License 2.0 5 votes vote down vote up
private void dumpSourceToStdOut(Source<?,NotUsed> src) throws InterruptedException, ExecutionException {
    final ActorSystem system = ActorSystem.create("QuickStart");
    final Materializer materializer = ActorMaterializer.create(system);

    final CompletionStage<Done> done = src.runWith(Sink.foreach(a -> System.out.println(a)),materializer);
    done.thenRun(()->system.terminate());

    // Make it happen
    done.toCompletableFuture().get();
}
 
Example 6
Source File: AkkaApplicationTest.java    From reactive-code-workshop with Apache License 2.0 5 votes vote down vote up
private String dumpSourceToString(Source<?,NotUsed> f) throws InterruptedException, ExecutionException {
    final ActorSystem system = ActorSystem.create("QuickStart");
    final Materializer materializer = ActorMaterializer.create(system);

    StringBuilder s = new StringBuilder();
    final CompletionStage<Done> done = f.runWith(Sink.foreach(a -> s.append(a)),materializer);

    done.thenRun(()->system.terminate());
    done.toCompletableFuture().get();

    return s.toString();
}
 
Example 7
Source File: AbstractCompletionStageTest.java    From completion-stage with Apache License 2.0 5 votes vote down vote up
@Test
public void thenRunShouldRun() {
    CompletionStage<String> completionStage = createCompletionStage(VALUE);

    Runnable runnable = mock(Runnable.class);
    completionStage.thenRun(runnable);

    finish(completionStage);

    verify(runnable).run();
}