本文整理汇总了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());
}