當前位置: 首頁>>代碼示例>>Java>>正文


Java CompletableFuture.anyOf方法代碼示例

本文整理匯總了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);
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:CompletableFutureTest.java

示例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));
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:21,代碼來源:CompletableFutureTest.java

示例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));
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:CompletableFutureTest.java

示例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);
}
 
開發者ID:sorokinigor,項目名稱:yet-another-try,代碼行數:14,代碼來源:AbstractRetryExecutorService.java

示例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);
}}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:CompletableFutureTest.java

示例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);
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:16,代碼來源:CompletableFutureTest.java

示例7: anyOfMyProcesses

import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
private CompletableFuture anyOfMyProcesses() {
    return CompletableFuture.anyOf(
            completableFuturesOfTheProcesses());
}
 
開發者ID:PacktPublishing,項目名稱:Mastering-Java-9,代碼行數:5,代碼來源:ControlDaemon.java


注:本文中的java.util.concurrent.CompletableFuture.anyOf方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。