Java Code Examples for java.util.concurrent.ScheduledExecutorService#invokeAll()

The following examples show how to use java.util.concurrent.ScheduledExecutorService#invokeAll() . These examples are extracted from open source projects. 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
@Test
public void invokeAllTest() throws InterruptedException, ExecutionException {
  ScheduledExecutorService scheduler = makeScheduler(THREAD_COUNT);
  try {
    List<TestCallable> toInvoke = new ArrayList<>(TEST_QTY);
    for (int i = 0; i < TEST_QTY; i++) {
      toInvoke.add(new TestCallable(0));
    }
    List<Future<Object>> result = scheduler.invokeAll(toInvoke);
    
    assertEquals(toInvoke.size(), result.size());
    
    Iterator<TestCallable> it = toInvoke.iterator();
    Iterator<Future<Object>> resultIt = result.iterator();
    while (it.hasNext()) {
      assertTrue(resultIt.next().get() == it.next().getReturnedResult());
    }
  } finally {
    scheduler.shutdownNow();
  }
}
 
Example 2
@Test
public void invokeAllTimeoutTest() throws InterruptedException {
  ScheduledExecutorService scheduler = makeScheduler(THREAD_COUNT);
  try {
    int runTime = 1000 * 10;
    int timeoutTime = 5;
    
    List<TestCallable> toInvoke = new ArrayList<>(TEST_QTY);
    for (int i = 0; i < TEST_QTY; i++) {
      toInvoke.add(new TestCallable(runTime));
    }
    
    long startTime = Clock.accurateForwardProgressingMillis();
    List<Future<Object>> result = scheduler.invokeAll(toInvoke, timeoutTime, TimeUnit.MILLISECONDS);
    long endTime = Clock.accurateForwardProgressingMillis();
    
    assertEquals(toInvoke.size(), result.size());
    
    assertTrue(endTime - startTime >= timeoutTime);
    assertTrue(endTime - startTime < timeoutTime + (SLOW_MACHINE ? 5000 : 500));
  } finally {
    scheduler.shutdownNow();
  }
}
 
Example 3
@Test
public void invokeAllExceptionTest() throws InterruptedException, ExecutionException {
  ScheduledExecutorService scheduler = makeScheduler(THREAD_COUNT);
  try {
    int exceptionCallableIndex = TEST_QTY / 2;
    
    List<TestCallable> toInvoke = new ArrayList<>(TEST_QTY);
    for (int i = 0; i < TEST_QTY; i++) {
      TestCallable tc;
      if (i == exceptionCallableIndex) {
        tc = new TestCallable(0) {
          @Override
          protected void handleCallStart() {
            throw new StackSuppressedRuntimeException();
          }
        };
      } else {
        tc = new TestCallable(0);
      }
      toInvoke.add(tc);
    }
    List<Future<Object>> result = scheduler.invokeAll(toInvoke);
    
    assertEquals(toInvoke.size(), result.size());
    
    Iterator<TestCallable> it = toInvoke.iterator();
    Iterator<Future<Object>> resultIt = result.iterator();
    for (int i = 0; i < TEST_QTY; i++) {
      if (i != exceptionCallableIndex) {
        assertTrue(resultIt.next().get() == it.next().getReturnedResult());
      } else {
        // skip fail entry
        resultIt.next();
        it.next();
      }
    }
  } finally {
    scheduler.shutdownNow();
  }
}
 
Example 4
@Test (expected = NullPointerException.class)
public void invokeAllFail() throws InterruptedException {
  ScheduledExecutorService scheduler = makeScheduler(1);
  try {
    List<TestCallable> toInvoke = new ArrayList<>(2);
    toInvoke.add(new TestCallable(0));
    toInvoke.add(null);
    scheduler.invokeAll(toInvoke);
  } finally {
    scheduler.shutdownNow();
  }
}