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


Java ExecutorService.invokeAny方法代碼示例

本文整理匯總了Java中java.util.concurrent.ExecutorService.invokeAny方法的典型用法代碼示例。如果您正苦於以下問題:Java ExecutorService.invokeAny方法的具體用法?Java ExecutorService.invokeAny怎麽用?Java ExecutorService.invokeAny使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.concurrent.ExecutorService的用法示例。


在下文中一共展示了ExecutorService.invokeAny方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testTimedInvokeAny5

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * timed invokeAny(c) returns result of some task
 */
public void testTimedInvokeAny5() throws Exception {
    final ExecutorService e =
        new CustomTPE(2, 2,
                      LONG_DELAY_MS, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(e)) {
        long startTime = System.nanoTime();
        List<Callable<String>> l = new ArrayList<>();
        l.add(new StringTask());
        l.add(new StringTask());
        String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
        assertSame(TEST_STRING, result);
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:ThreadPoolExecutorSubclassTest.java

示例2: testInvokeAny4

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * invokeAny(c) throws ExecutionException if no task completes
 */
public void testInvokeAny4() throws Exception {
    final ExecutorService e =
        new ThreadPoolExecutor(2, 2,
                               LONG_DELAY_MS, MILLISECONDS,
                               new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<>();
        l.add(new NPETask());
        try {
            e.invokeAny(l);
            shouldThrow();
        } catch (ExecutionException success) {
            assertTrue(success.getCause() instanceof NullPointerException);
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:ThreadPoolExecutorTest.java

示例3: testTimedInvokeAny4

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * timed invokeAny(c) throws ExecutionException if no task completes
 */
public void testTimedInvokeAny4() throws Exception {
    final ExecutorService e =
        new CustomTPE(2, 2,
                      LONG_DELAY_MS, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(e)) {
        long startTime = System.nanoTime();
        List<Callable<String>> l = new ArrayList<>();
        l.add(new NPETask());
        try {
            e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
            shouldThrow();
        } catch (ExecutionException success) {
            assertTrue(success.getCause() instanceof NullPointerException);
        }
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:ThreadPoolExecutorSubclassTest.java

示例4: testTimedInvokeAny5

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * timed invokeAny(c) returns result of some task
 */
public void testTimedInvokeAny5() throws Exception {
    final ExecutorService e =
        new ThreadPoolExecutor(2, 2,
                               LONG_DELAY_MS, MILLISECONDS,
                               new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(e)) {
        long startTime = System.nanoTime();
        List<Callable<String>> l = new ArrayList<>();
        l.add(new StringTask());
        l.add(new StringTask());
        String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
        assertSame(TEST_STRING, result);
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:ThreadPoolExecutorTest.java

示例5: testTimedInvokeAny4

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * timed invokeAny(c) throws ExecutionException if no task completes
 */
public void testTimedInvokeAny4() throws Exception {
    final ExecutorService e = new ScheduledThreadPoolExecutor(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        long startTime = System.nanoTime();
        List<Callable<String>> l = new ArrayList<>();
        l.add(new NPETask());
        try {
            e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
            shouldThrow();
        } catch (ExecutionException success) {
            assertTrue(success.getCause() instanceof NullPointerException);
        }
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:ScheduledExecutorTest.java

示例6: testInvokeAny6

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * invokeAny(c) returns result of some task in c if at least one completes
 */
public void testInvokeAny6() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<>();
        l.add(new StringTask());
        l.add(new StringTask());
        String result = e.invokeAny(l);
        assertSame(TEST_STRING, result);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:ForkJoinPoolTest.java

示例7: testTimedInvokeAny3

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * timed invokeAny(c) throws NPE if c has null elements
 */
public void testTimedInvokeAny3() throws Exception {
    final ExecutorService e = new DirectExecutorService();
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<Long>> l = new ArrayList<>();
        l.add(new Callable<Long>() {
                  public Long call() { throw new ArithmeticException(); }});
        l.add(null);
        try {
            e.invokeAny(l, randomTimeout(), randomTimeUnit());
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,代碼來源:AbstractExecutorServiceTest.java

示例8: testInvokeAny

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
@Test
public void testInvokeAny() throws InterruptedException, ExecutionException {
  ExecutorService executorService = toTraced(Executors.newFixedThreadPool(NUMBER_OF_THREADS));

  MockSpan parentSpan = mockTracer.buildSpan("foo").startManual();
  mockTracer.scopeManager().activate(parentSpan, true);
  executorService.invokeAny(Arrays.asList(new TestCallable()));

  countDownLatch.await();
  assertParentSpan(parentSpan);
  assertEquals(1, mockTracer.finishedSpans().size());
}
 
開發者ID:opentracing-contrib,項目名稱:java-concurrent,代碼行數:13,代碼來源:TracedExecutorServiceTest.java

示例9: testInvokeAny1

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * invokeAny(null) throws NPE
 */
public void testInvokeAny1() throws Exception {
    final ExecutorService e = new CustomExecutor(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        try {
            e.invokeAny(null);
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:ScheduledExecutorSubclassTest.java

示例10: testTimedInvokeAny3

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * timed invokeAny(c) throws NPE if c has null elements
 */
public void testTimedInvokeAny3() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final ExecutorService e = new ScheduledThreadPoolExecutor(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<>();
        l.add(latchAwaitingStringTask(latch));
        l.add(null);
        try {
            e.invokeAny(l, randomTimeout(), randomTimeUnit());
            shouldThrow();
        } catch (NullPointerException success) {}
        latch.countDown();
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:ScheduledExecutorTest.java

示例11: testInvokeAny3

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * invokeAny(c) throws NPE if c has null elements
 */
public void testInvokeAny3() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final ExecutorService e = new CustomExecutor(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<>();
        l.add(latchAwaitingStringTask(latch));
        l.add(null);
        try {
            e.invokeAny(l);
            shouldThrow();
        } catch (NullPointerException success) {}
        latch.countDown();
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:ScheduledExecutorSubclassTest.java

示例12: testTimedInvokeAny5

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * timed invokeAny(c) returns result of some task
 */
public void testTimedInvokeAny5() throws Exception {
    final ExecutorService e = new ScheduledThreadPoolExecutor(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        long startTime = System.nanoTime();
        List<Callable<String>> l = new ArrayList<>();
        l.add(new StringTask());
        l.add(new StringTask());
        String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
        assertSame(TEST_STRING, result);
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:16,代碼來源:ScheduledExecutorTest.java

示例13: testInvokeAny4

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * invokeAny(c) throws ExecutionException if no task completes
 */
public void testInvokeAny4() throws Exception {
    final ExecutorService e = new CustomExecutor(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<>();
        l.add(new NPETask());
        try {
            e.invokeAny(l);
            shouldThrow();
        } catch (ExecutionException success) {
            assertTrue(success.getCause() instanceof NullPointerException);
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,代碼來源:ScheduledExecutorSubclassTest.java

示例14: testInvokeAny5

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * invokeAny(c) returns result of some task in c if at least one completes
 */
public void testInvokeAny5() throws Exception {
    final ExecutorService e = new DirectExecutorService();
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<>();
        l.add(new StringTask());
        l.add(new StringTask());
        String result = e.invokeAny(l);
        assertSame(TEST_STRING, result);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:AbstractExecutorServiceTest.java

示例15: testTimedInvokeAny1

import java.util.concurrent.ExecutorService; //導入方法依賴的package包/類
/**
 * timed invokeAny(null) throws NPE
 */
public void testTimedInvokeAny1() throws Exception {
    final ExecutorService e = new ScheduledThreadPoolExecutor(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        try {
            e.invokeAny(null, randomTimeout(), randomTimeUnit());
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:ScheduledExecutorTest.java


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