java.util.concurrent.ScheduledExecutorService#invokeAll ( )源码实例Demo

下面列出了java.util.concurrent.ScheduledExecutorService#invokeAll ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: threadly   文件: ScheduledExecutorServiceTest.java
@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();
  }
}
 
源代码2 项目: threadly   文件: ScheduledExecutorServiceTest.java
@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();
  }
}
 
源代码3 项目: threadly   文件: ScheduledExecutorServiceTest.java
@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();
  }
}
 
源代码4 项目: threadly   文件: ScheduledExecutorServiceTest.java
@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();
  }
}