本文整理匯總了Java中java.util.concurrent.CompletableFuture.anyOf方法的典型用法代碼示例。如果您正苦於以下問題:Java CompletableFuture.anyOf方法的具體用法?Java CompletableFuture.anyOf怎麽用?Java CompletableFuture.anyOf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.CompletableFuture
的用法示例。
在下文中一共展示了CompletableFuture.anyOf方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testAnyOf_normal
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* anyOf returns a future completed normally with a value when
* a component future does
*/
public void testAnyOf_normal() throws Exception {
for (int k = 0; k < 10; k++) {
CompletableFuture[] fs = new CompletableFuture[k];
for (int i = 0; i < k; i++)
fs[i] = new CompletableFuture<>();
CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
checkIncomplete(f);
for (int i = 0; i < k; i++) {
fs[i].complete(i);
checkCompletedNormally(f, 0);
int x = (int) CompletableFuture.anyOf(fs).join();
assertTrue(0 <= x && x <= i);
}
}
}
示例2: testAnyOf_exceptional
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* anyOf result completes exceptionally when any component does.
*/
public void testAnyOf_exceptional() throws Exception {
for (int k = 0; k < 10; k++) {
CompletableFuture[] fs = new CompletableFuture[k];
CFException[] exs = new CFException[k];
for (int i = 0; i < k; i++) {
fs[i] = new CompletableFuture<>();
exs[i] = new CFException();
}
CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
checkIncomplete(f);
for (int i = 0; i < k; i++) {
fs[i].completeExceptionally(exs[i]);
checkCompletedWithWrappedException(f, exs[0]);
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
}
}
}
示例3: testAnyOf_exceptional_backwards
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
public void testAnyOf_exceptional_backwards() throws Exception {
for (int k = 0; k < 10; k++) {
CompletableFuture[] fs = new CompletableFuture[k];
CFException[] exs = new CFException[k];
for (int i = 0; i < k; i++) {
fs[i] = new CompletableFuture<>();
exs[i] = new CFException();
}
CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
checkIncomplete(f);
for (int i = k - 1; i >= 0; i--) {
fs[i].completeExceptionally(exs[i]);
checkCompletedWithWrappedException(f, exs[k - 1]);
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
}
}
}
示例4: doInvokeAny
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
private <T> CompletableFuture<T> doInvokeAny(Collection<? extends Callable<T>> tasks) {
Objects.requireNonNull(tasks, "'tasks' should not be 'null'.");
if (tasks.isEmpty()) {
throw new IllegalArgumentException("'tasks' should not be empty.");
}
CompletableFuture[] futures = new CompletableFuture[tasks.size()];
int index = 0;
for (Callable<T> task : tasks) {
futures[index++] = submit(task);
}
return (CompletableFuture<T>) CompletableFuture.anyOf(futures);
}
示例5: testAnyOf_empty
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* anyOf(no component futures) returns an incomplete future
*/
public void testAnyOf_empty() throws Exception {
for (Integer v1 : new Integer[] { 1, null })
{
CompletableFuture<Object> f = CompletableFuture.anyOf();
checkIncomplete(f);
f.complete(v1);
checkCompletedNormally(f, v1);
}}
示例6: testAnyOf_normal_backwards
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
public void testAnyOf_normal_backwards() throws Exception {
for (int k = 0; k < 10; k++) {
CompletableFuture[] fs = new CompletableFuture[k];
for (int i = 0; i < k; i++)
fs[i] = new CompletableFuture<>();
CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
checkIncomplete(f);
for (int i = k - 1; i >= 0; i--) {
fs[i].complete(i);
checkCompletedNormally(f, k - 1);
int x = (int) CompletableFuture.anyOf(fs).join();
assertTrue(i <= x && x <= k - 1);
}
}
}
示例7: anyOfMyProcesses
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
private CompletableFuture anyOfMyProcesses() {
return CompletableFuture.anyOf(
completableFuturesOfTheProcesses());
}