Java Code Examples for java.util.concurrent.ExecutorService#invokeAny()

The following examples show how to use java.util.concurrent.ExecutorService#invokeAny() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThreadPoolExecutorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 2
Source File: GoogleHadoopFileSystemBase.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
/**
 * Use 2 glob algorithms that return the same result but one of them could be significantly faster
 * than another one depending on directory layout.
 */
private FileStatus[] concurrentGlobInternal(Path fixedPath, PathFilter filter)
    throws IOException {
  ExecutorService executorService = Executors.newFixedThreadPool(2, DAEMON_THREAD_FACTORY);
  Callable<FileStatus[]> flatGlobTask = () -> flatGlobInternal(fixedPath, filter);
  Callable<FileStatus[]> nonFlatGlobTask = () -> super.globStatus(fixedPath, filter);

  try {
    return executorService.invokeAny(Arrays.asList(flatGlobTask, nonFlatGlobTask));
  } catch (InterruptedException | ExecutionException e) {
    if (e instanceof InterruptedException) {
      Thread.currentThread().interrupt();
    }
    throw new IOException("Concurrent glob execution failed", e);
  } finally {
    executorService.shutdownNow();
  }
}
 
Example 3
Source File: ThreadPoolExecutorSubclassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 4
Source File: ForkJoinPoolTest.java    From streamsupport with GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAny(c) throws NullPointerException if c has a single null element
 */
public void testInvokeAny3() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        List<Callable<String>> l = new ArrayList<>();
        l.add(null);
        try {
            e.invokeAny(l);
            shouldThrow();
        } catch (NullPointerException success) {}
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
 
Example 5
Source File: ThreadPoolExecutorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAny(c) throws NPE if c has null elements
 */
public void testInvokeAny3() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    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(latchAwaitingStringTask(latch));
        l.add(null);
        try {
            e.invokeAny(l);
            shouldThrow();
        } catch (NullPointerException success) {}
        latch.countDown();
    }
}
 
Example 6
Source File: ForkJoinPoolTest.java    From streamsupport with GNU General Public License v2.0 6 votes vote down vote up
/**
 * timed invokeAny(null time unit) throws NullPointerException
 */
public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        List<Callable<String>> l = new ArrayList<>();
        l.add(new StringTask());
        try {
            e.invokeAny(l, randomTimeout(), null);
            shouldThrow();
        } catch (NullPointerException success) {}
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
 
Example 7
Source File: ForkJoinPoolTest.java    From streamsupport with GNU General Public License v2.0 6 votes vote down vote up
/**
 * timed invokeAny(null) throws NullPointerException
 */
public void testTimedInvokeAny1() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        try {
            e.invokeAny(null, randomTimeout(), randomTimeUnit());
            shouldThrow();
        } catch (NullPointerException success) {}
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
 
Example 8
Source File: ThreadPoolExecutorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * invokeAny(c) throws NPE if c has null elements
 */
public void testInvokeAny3() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    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<Callable<String>>();
        l.add(latchAwaitingStringTask(latch));
        l.add(null);
        try {
            e.invokeAny(l);
            shouldThrow();
        } catch (NullPointerException success) {}
        latch.countDown();
    }
}
 
Example 9
Source File: ScheduledExecutorSubclassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * invokeAny(empty collection) throws IAE
 */
public void testInvokeAny2() throws Exception {
    final ExecutorService e = new CustomExecutor(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        try {
            e.invokeAny(new ArrayList<Callable<String>>());
            shouldThrow();
        } catch (IllegalArgumentException success) {}
    }
}
 
Example 10
Source File: ScheduledExecutorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * invokeAny(null) throws NPE
 */
public void testInvokeAny1() throws Exception {
    final ExecutorService e = new ScheduledThreadPoolExecutor(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        try {
            e.invokeAny(null);
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}
 
Example 11
Source File: ThreadPoolExecutorSubclassTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * timed invokeAny(empty collection) throws IAE
 */
public void testTimedInvokeAny2() throws Exception {
    final ExecutorService e =
        new CustomTPE(2, 2,
                      LONG_DELAY_MS, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(e)) {
        try {
            e.invokeAny(new ArrayList<Callable<String>>(),
                        MEDIUM_DELAY_MS, MILLISECONDS);
            shouldThrow();
        } catch (IllegalArgumentException success) {}
    }
}
 
Example 12
Source File: AbstractExecutorServiceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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, MEDIUM_DELAY_MS, MILLISECONDS);
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}
 
Example 13
Source File: ScheduledExecutorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * invokeAny(c) throws NPE if c has null elements
 */
public void testInvokeAny3() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final ExecutorService e = new ScheduledThreadPoolExecutor(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<Callable<String>>();
        l.add(latchAwaitingStringTask(latch));
        l.add(null);
        try {
            e.invokeAny(l);
            shouldThrow();
        } catch (NullPointerException success) {}
        latch.countDown();
    }
}
 
Example 14
Source File: ScheduledExecutorSubclassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * timed invokeAny(null) throws NPE
 */
public void testTimedInvokeAny1() throws Exception {
    final ExecutorService e = new CustomExecutor(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        try {
            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}
 
Example 15
Source File: ForkJoinPoolTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * invokeAny(c) throws NullPointerException if c has null elements
 */
public void testInvokeAny4() throws Throwable {
    CountDownLatch latch = new CountDownLatch(1);
    ExecutorService e = new ForkJoinPool(1);
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<Callable<String>>();
        l.add(latchAwaitingStringTask(latch));
        l.add(null);
        try {
            e.invokeAny(l);
            shouldThrow();
        } catch (NullPointerException success) {}
        latch.countDown();
    }
}
 
Example 16
Source File: ScheduledExecutorSubclassTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * timed invokeAny(,,null) throws NPE
 */
public void testTimedInvokeAnyNullTimeUnit() throws Exception {
    final ExecutorService e = new CustomExecutor(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<Callable<String>>();
        l.add(new StringTask());
        try {
            e.invokeAny(l, MEDIUM_DELAY_MS, null);
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}
 
Example 17
Source File: ForkJoinPoolTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * invokeAny(c) throws NullPointerException if c has a single null element
 */
public void testInvokeAny3() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<>();
        l.add(null);
        try {
            e.invokeAny(l);
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}
 
Example 18
Source File: ScheduledExecutorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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<Callable<String>>();
        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);
    }
}
 
Example 19
Source File: ThreadPoolExecutorSubclassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * timed invokeAny(empty collection) throws IAE
 */
public void testTimedInvokeAny2() throws Exception {
    final ExecutorService e =
        new CustomTPE(2, 2,
                      LONG_DELAY_MS, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(e)) {
        try {
            e.invokeAny(new ArrayList<Callable<String>>(),
                        MEDIUM_DELAY_MS, MILLISECONDS);
            shouldThrow();
        } catch (IllegalArgumentException success) {}
    }
}
 
Example 20
Source File: ForkJoinPoolTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * timed invokeAny(null time unit) throws NullPointerException
 */
public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<Callable<String>>();
        l.add(new StringTask());
        try {
            e.invokeAny(l, MEDIUM_DELAY_MS, null);
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}