本文整理匯總了Java中java.util.concurrent.CompletableFuture.complete方法的典型用法代碼示例。如果您正苦於以下問題:Java CompletableFuture.complete方法的具體用法?Java CompletableFuture.complete怎麽用?Java CompletableFuture.complete使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.CompletableFuture
的用法示例。
在下文中一共展示了CompletableFuture.complete方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testOnPingIncorrect
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
public void testOnPingIncorrect(boolean fin, boolean rsv1, boolean rsv2,
boolean rsv3, ByteBuffer data) {
if (fin && !rsv1 && !rsv2 && !rsv3 && data.remaining() <= 125) {
throw new SkipException("Correct frame");
}
CompletableFuture<WebSocket> webSocket = new CompletableFuture<>();
MockChannel channel = new MockChannel.Builder()
.provideFrame(fin, rsv1, rsv2, rsv3, Opcode.PING, data)
.expectClose((code, reason) ->
Integer.valueOf(1002).equals(code) && "".equals(reason))
.build();
MockListener listener = new MockListener.Builder()
.expectOnOpen((ws) -> true)
.expectOnError((ws, error) -> error instanceof ProtocolException)
.build();
webSocket.complete(newWebSocket(channel, listener));
checkExpectations(500, TimeUnit.MILLISECONDS, channel, listener);
}
示例2: handle
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
private Void handle(CompletableFuture<Response<OutputT>> future,
Response<OutputT> resp,
Throwable err) {
try {
if (resp != null && resp.isSuccess()) {
retryHandler.releaseRetryCapacity();
future.complete(resp);
} else if (resp != null) {
retryHandler.setLastRetriedException(handleSdkException(resp));
executeRetry(future);
} else {
SdkClientException exception = new SdkClientException(err);
retryHandler.setLastRetriedException(handleSdkException(Response.fromFailure(exception, null)));
executeRetry(future);
}
} catch (Exception e) {
future.completeExceptionally(e);
}
return null;
}
示例3: readContent
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Override
protected void readContent(ByteBuf in, CompletableFuture<Long> promise) {
try {
int before = in.readableBytes();
sink.write(in);
int after = in.readableBytes();
readed += before - after;
//處理 內容長度
if (readed >= length) {
promise.complete(readed);
}
} catch (IOException e) {
throw new FastdfsException("write response to output error.", e);
}
}
示例4: flatMapPromiseN
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Benchmark
public String flatMapPromiseN() throws InterruptedException, ExecutionException {
CompletableFuture<String> p = new CompletableFuture<>();
CompletableFuture<String> f = p;
for (int i = 0; i < N.n; i++)
f = f.thenCompose(flatMapF);
p.complete(string);
return f.get();
}
示例5: Connection
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
Connection(EventLoopGroup group) throws InterruptedException {
final ChannelInboundHandler inboundHandler
= new SimpleChannelInboundHandler<RaftNettyServerReplyProto>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx,
RaftNettyServerReplyProto proto) {
final CompletableFuture<RaftNettyServerReplyProto> future = pollReply();
if (future == null) {
throw new IllegalStateException("Request #" + getCallId(proto)
+ " not found");
}
if (proto.getRaftNettyServerReplyCase() == EXCEPTIONREPLY) {
final Object ioe = ProtoUtils.toObject(proto.getExceptionReply().getException());
future.completeExceptionally((IOException)ioe);
} else {
future.complete(proto);
}
}
};
final ChannelInitializer<SocketChannel> initializer
= new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
final ChannelPipeline p = ch.pipeline();
p.addLast(new ProtobufVarint32FrameDecoder());
p.addLast(new ProtobufDecoder(RaftNettyServerReplyProto.getDefaultInstance()));
p.addLast(new ProtobufVarint32LengthFieldPrepender());
p.addLast(new ProtobufEncoder());
p.addLast(inboundHandler);
}
};
client.connect(peer.getAddress(), group, initializer);
}
示例6: mapPromiseN
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Benchmark
public String mapPromiseN() throws InterruptedException, ExecutionException {
CompletableFuture<String> p = new CompletableFuture<String>();
CompletableFuture<String> f = p;
for (int i = 0; i < N.n; i++)
f = f.thenApplyAsync(mapF);
p.complete(string);
return f.get();
}
示例7: ensurePromise
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Benchmark
public Void ensurePromise() throws InterruptedException, ExecutionException {
CompletableFuture<Void> p = new CompletableFuture<Void>();
CompletableFuture<Void> f = p.thenRun(ensureF);
p.complete(null);
return f.get();
}
示例8: testPropagateCancellation_completedResultNotPropagated
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test public void testPropagateCancellation_completedResultNotPropagated() {
CompletableFuture<String> outer = new CompletableFuture<>();
CompletableFuture<String> inner = new CompletableFuture<>();
assertThat(Utils.propagateCancellation(outer, inner)).isSameAs(outer);
outer.complete("ok");
assertThat(outer.isCancelled()).isFalse();
assertThat(inner.isCancelled()).isFalse();
assertThat(outer.isDone()).isTrue();
assertThat(inner.isDone()).isFalse();
}
示例9: flatMapPromise
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Benchmark
public String flatMapPromise() throws InterruptedException, ExecutionException {
CompletableFuture<String> p = new CompletableFuture<String>();
p.thenCompose(flatMapF);
p.complete(string);
return p.get();
}
示例10: wrapFuture_futureBecomesSuccess
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test public void wrapFuture_futureBecomesSuccess() throws Exception {
CompletableFuture<String> future = new CompletableFuture<>();
CompletionStage<Maybe<String, Exception>> stage = Maybe.catchException(Exception.class, future);
assertPending(stage);
future.complete("good");
assertCompleted(stage).isEqualTo(Maybe.of("good"));
}
示例11: testNotCompletedThenComposeCancelled
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* This test is one part of verifying https://github.com/tunnelvisionlabs/java-threading/issues/11.
*/
@Test
public void testNotCompletedThenComposeCancelled() {
CompletableFuture<Void> future = new CompletableFuture<>();
CompletableFuture<Void> composed = future.thenCompose(s -> Futures.completedCancelled());
future.complete(null);
Assert.assertTrue(composed.isDone());
Assert.assertTrue(composed.isCompletedExceptionally());
Assert.assertFalse("Cancellation is not preserved when the antecedent is not completed when thenCompose is called", composed.isCancelled());
thrown.expect(CompletionException.class);
thrown.expectCause(isA(CancellationException.class));
composed.join();
}
示例12: testNewIncompleteFuture
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* newIncompleteFuture returns an incomplete CompletableFuture
*/
public void testNewIncompleteFuture() {
for (Integer v1 : new Integer[] { 1, null })
{
CompletableFuture<Integer> f = new CompletableFuture<>();
CompletableFuture<Integer> g = f.newIncompleteFuture();
checkIncomplete(f);
checkIncomplete(g);
f.complete(v1);
checkCompletedNormally(f, v1);
checkIncomplete(g);
g.complete(v1);
checkCompletedNormally(g, v1);
assertSame(g.getClass(), CompletableFuture.class);
}}
示例13: requestComplete
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
synchronized void requestComplete() {
number--;
// don't unblock until number of requests has halved.
if ((blocked && number <= maxnumber / 2) ||
(!blocked && waiters.size() > 0)) {
int toRelease = Math.min(maxnumber - number, waiters.size());
for (int i=0; i<toRelease; i++) {
CompletableFuture<Void> f = waiters.remove();
number ++;
f.complete(null);
}
blocked = number >= maxnumber;
}
}
示例14: testRunAfterBoth_exceptionalCompletion
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* runAfterBoth result completes exceptionally after exceptional
* completion of either source
*/
public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
for (ExecutionMode m : ExecutionMode.values())
for (boolean fFirst : new boolean[] { true, false })
for (boolean failFirst : new boolean[] { true, false })
for (Integer v1 : new Integer[] { 1, null })
{
final CompletableFuture<Integer> f = new CompletableFuture<>();
final CompletableFuture<Integer> g = new CompletableFuture<>();
final CFException ex = new CFException();
final Noop r1 = new Noop(m);
final Noop r2 = new Noop(m);
final Noop r3 = new Noop(m);
final CompletableFuture<Integer> fst = fFirst ? f : g;
final CompletableFuture<Integer> snd = !fFirst ? f : g;
final Callable<Boolean> complete1 = failFirst ?
() -> fst.completeExceptionally(ex) :
() -> fst.complete(v1);
final Callable<Boolean> complete2 = failFirst ?
() -> snd.complete(v1) :
() -> snd.completeExceptionally(ex);
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
assertTrue(complete1.call());
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
checkIncomplete(h1);
checkIncomplete(h2);
assertTrue(complete2.call());
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
checkCompletedWithWrappedException(h1, ex);
checkCompletedWithWrappedException(h2, ex);
checkCompletedWithWrappedException(h3, ex);
r1.assertNotInvoked();
r2.assertNotInvoked();
r3.assertNotInvoked();
checkCompletedNormally(failFirst ? snd : fst, v1);
checkCompletedExceptionally(failFirst ? fst : snd, ex);
}}
示例15: testCompleteOnTimeout_completed
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* completeOnTimeout has no effect if completed within timeout
*/
public void testCompleteOnTimeout_completed() {
for (Integer v1 : new Integer[] { 1, null })
{
CompletableFuture<Integer> f = new CompletableFuture<>();
CompletableFuture<Integer> g = new CompletableFuture<>();
long startTime = System.nanoTime();
f.complete(v1);
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
g.complete(v1);
checkCompletedNormally(f, v1);
checkCompletedNormally(g, v1);
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
}}