本文整理匯總了Java中java.util.concurrent.CompletableFuture.isDone方法的典型用法代碼示例。如果您正苦於以下問題:Java CompletableFuture.isDone方法的具體用法?Java CompletableFuture.isDone怎麽用?Java CompletableFuture.isDone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.CompletableFuture
的用法示例。
在下文中一共展示了CompletableFuture.isDone方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: completeResponse
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* Completes the first uncompleted CF on list, and removes it. If there is no
* uncompleted CF then creates one (completes it) and adds to list
*/
void completeResponse(Response resp) {
synchronized (response_cfs) {
CompletableFuture<Response> cf;
int cfs_len = response_cfs.size();
for (int i=0; i<cfs_len; i++) {
cf = response_cfs.get(i);
if (!cf.isDone()) {
Log.logTrace("Completing response (streamid={0}): {1}",
streamid, cf);
cf.complete(resp);
response_cfs.remove(cf);
return;
} // else we found the previous response: just leave it alone.
}
cf = MinimalFuture.completedFuture(resp);
Log.logTrace("Created completed future (streamid={0}): {1}",
streamid, cf);
response_cfs.add(cf);
}
}
示例2: close
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Override
public void close() {
state.set(State.STOPPED);
if (!initialValueFuture.isDone()) {
initialValueFuture.cancel(false);
}
// Cancel any scheduled operations.
final ScheduledFuture<?> currentScheduleFuture = this.currentScheduleFuture;
if (currentScheduleFuture != null && !currentScheduleFuture.isDone()) {
currentScheduleFuture.cancel(false);
}
final CompletableFuture<?> currentWatchFuture = this.currentWatchFuture;
if (currentWatchFuture != null && !currentWatchFuture.isDone()) {
currentWatchFuture.cancel(false);
}
}
示例3: supportsCompletableFuturesAsReturnTypeWrapper
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* Here we demonstrate the usage of {@link CompletableFuture} as a result wrapper for asynchronous repository query
* methods. Note, that we need to disable the surrounding transaction to be able to asynchronously read the written
* data from from another thread within the same test method.
*/
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void supportsCompletableFuturesAsReturnTypeWrapper() throws Exception {
repository.save(new Customer("Customer1", "Foo"));
repository.save(new Customer("Customer2", "Bar"));
CompletableFuture<Void> future = repository.readAllBy().thenAccept(customers -> {
assertThat(customers, hasSize(2));
customers.forEach(customer -> log.info(customer.toString()));
log.info("Completed!");
});
while (!future.isDone()) {
log.info("Waiting for the CompletableFuture to finish...");
TimeUnit.MILLISECONDS.sleep(500);
}
future.get();
log.info("Done!");
}
示例4: testFindDetails
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test
public void testFindDetails() throws InterruptedException, ExecutionException {
CompletableFuture<Concept> f0 = conceptService.findRoot();
while (!f0.isDone()) {
Thread.sleep(20);
}
Concept c = f0.get();
assertNotNull(c);
CompletableFuture<Optional<ConceptDetails>> f1 = conceptService.findDetails(c.getName());
while (!f1.isDone()) {
Thread.sleep(20);
}
Optional<ConceptDetails> cd = f1.get();
assertTrue(cd.isPresent());
CompletableFuture<Optional<ConceptDetails>> f2 = conceptService.findDetails("Pandalus platyceros");
while (!f2.isDone()) {
Thread.sleep(20);
}
Optional<ConceptDetails> n = f2.get();
assertTrue(n.isPresent());
}
示例5: completeResponseExceptionally
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* same as above but for errors
*/
void completeResponseExceptionally(Throwable t) {
synchronized (response_cfs) {
// use index to avoid ConcurrentModificationException
// caused by removing the CF from within the loop.
for (int i = 0; i < response_cfs.size(); i++) {
CompletableFuture<Response> cf = response_cfs.get(i);
if (!cf.isDone()) {
cf.completeExceptionally(t);
response_cfs.remove(i);
return;
}
}
response_cfs.add(MinimalFuture.failedFuture(t));
}
}
示例6: latestValue
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* Returns the latest value of {@code watchFile()} result.
*
* @param defaultValue the default value which is returned when the value is not available yet
*/
@Nullable
default T latestValue(@Nullable T defaultValue) {
final CompletableFuture<Latest<T>> initialValueFuture = initialValueFuture();
if (initialValueFuture.isDone() && !initialValueFuture.isCompletedExceptionally()) {
return latest().value();
} else {
return defaultValue;
}
}
示例7: watchRepository
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* Awaits and retrieves the latest revision of the commit that changed the file that matches the specified
* {@code pathPattern} since the specified {@code lastKnownRevision}. This will wait until the specified
* {@code timeoutMillis} passes. If there's no change during the time, the returned future will be
* exceptionally completed with the {@link CancellationException}.
*/
public CompletableFuture<Revision> watchRepository(Repository repo, Revision lastKnownRevision,
String pathPattern, long timeoutMillis) {
final CompletableFuture<Revision> result = repo.watch(lastKnownRevision, pathPattern);
if (result.isDone()) {
return result;
}
scheduleTimeout(result, timeoutMillis);
return result;
}
示例8: watchFile
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* Awaits and retrieves the latest revision of the commit that changed the file that matches the specified
* {@link Query} since the specified {@code lastKnownRevision}. This will wait until the specified
* {@code timeoutMillis} passes. If there's no change during the time, the returned future will be
* exceptionally completed with the {@link CancellationException}.
*/
public <T> CompletableFuture<QueryResult<T>> watchFile(Repository repo, Revision lastKnownRevision,
Query<T> query, long timeoutMillis) {
final CompletableFuture<QueryResult<T>> result = repo.watch(lastKnownRevision, query);
if (result.isDone()) {
return result;
}
scheduleTimeout(result, timeoutMillis);
return result;
}
示例9: futureOf
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Override
public CompletableFuture<ClusterTopology> futureOf(Predicate<ClusterTopology> predicate) {
ArgAssert.notNull(predicate, "Predicate");
CompletableFuture<ClusterTopology> future = new CompletableFuture<>();
ClusterEventListener listener = new ClusterEventListener() {
@Override
public void onEvent(ClusterEvent event) {
InitializationContext localCtx = requireContext();
if (future.isDone()) {
localCtx.cluster().removeListener(this);
} else if (predicate.test(event.topology())) {
localCtx.cluster().removeListener(this);
future.complete(event.topology());
} else if (event.type() == ClusterEventType.LEAVE) {
localCtx.cluster().removeListener(this);
future.cancel(false);
}
}
};
guard.lockRead();
try {
if (guard.isInitialized()) {
requireContext().cluster().addListenerAsync(listener);
} else {
deferredListeners.add(new DeferredListener(listener, null));
}
} finally {
guard.unlockRead();
}
return future;
}
示例10: completeReplyExceptionally
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
private void completeReplyExceptionally(Throwable t, String event) {
appendStreamObservers.compareAndSet(this, null);
final Map<Long, CompletableFuture<RaftClientReply>> map = replies.getAndSet(null);
if (map == null) {
return;
}
for (Map.Entry<Long, CompletableFuture<RaftClientReply>> entry : map.entrySet()) {
final CompletableFuture<RaftClientReply> f = entry.getValue();
if (!f.isDone()) {
f.completeExceptionally(t != null? t
: new IOException(getName() + ": Stream " + event
+ ": no reply for async request cid=" + entry.getKey()));
}
}
}
示例11: acceptAndWait
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* Creates a transceiver and waits for receiving a stop signal. The method blocks until the future passed
* to the preparation consumer is completed. It's up to the consumer on which event the transceiver should stop
* @param address
* the address to listen for packets
* @param listenPort
* the port to listen for packets (UDP)
* @param prepare
* a consumer that gets the instance of the transceiver in order to setup proper actions on receiving events
*/
public static void acceptAndWait(InetAddress address, int listenPort, BiConsumer<SignalTransceiver, CompletableFuture<Event>> prepare){
try (SignalTransceiver com = SignalTransceiver.create(address, listenPort).start()) {
CompletableFuture<Event> stopListening = new CompletableFuture<>();
prepare.accept(com, stopListening);
while (!stopListening.isDone() || stopListening.isCancelled()) {
Thread.sleep(150);
}
} catch (Exception e) {
throw new RuntimeException("Failure to receive signals", e);
}
}
示例12: testValueFactoryExecutedOnlyOnceConcurrent
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* Verifies that multiple concurrent calls to {@link AsyncLazy#getValueAsync()} do not result in multiple
* invocations of the value factory.
*/
private void testValueFactoryExecutedOnlyOnceConcurrent(boolean specifyJtf) {
// use our own so we don't get main thread deadlocks, which isn't the point of this test.
JoinableFutureFactory jtf = specifyJtf ? new JoinableFutureContext().getFactory() : null;
CompletableFuture<Void> cts = Async.delayAsync(ASYNC_DELAY);
while (!cts.isDone()) {
// for debugging purposes only
AtomicBoolean valueFactoryResumed = new AtomicBoolean(false);
AtomicBoolean valueFactoryExecuted = new AtomicBoolean(false);
AsyncLazy<GenericParameterHelper> lazy = new AsyncLazy<>(
() -> {
Assert.assertFalse(valueFactoryExecuted.get());
valueFactoryExecuted.set(true);
return Async.awaitAsync(
Async.yieldAsync(),
() -> {
valueFactoryResumed.set(true);
return CompletableFuture.completedFuture(new GenericParameterHelper(5));
});
},
jtf);
List<GenericParameterHelper> results = TestUtilities.<GenericParameterHelper>concurrencyTest(() -> lazy.getValueAsync().join());
Assert.assertEquals(5, results.get(0).getData());
for (int i = 1; i < results.size(); i++) {
Assert.assertSame(results.get(0), results.get(i));
}
}
}
示例13: testFetchConceptTree
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test
public void testFetchConceptTree() throws InterruptedException, ExecutionException {
CompletableFuture<Concept> f = conceptService.findRoot();
while (!f.isDone()) {
Thread.sleep(20);
}
Concept c = f.get();
assertNotNull(c);
}
示例14: testFetchConceptTree
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test
public void testFetchConceptTree() throws InterruptedException, ExecutionException {
CompletableFuture<Concept> f = conceptService.findRoot();
while (!f.isDone()) {
Thread.sleep(20);
}
Concept c = f.get();
assertNotNull(c);
}
示例15: getResponseAsync
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Override
CompletableFuture<Response> getResponseAsync(Executor executor) {
CompletableFuture<Response> cf = pushCF.whenComplete((v, t) -> pushGroup.pushError(t));
if(executor!=null && !cf.isDone()) {
cf = cf.thenApplyAsync( r -> r, executor);
}
return cf;
}