当前位置: 首页>>代码示例>>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;未经允许,请勿转载。