java.util.concurrent.CancellationException.getCause()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(6.4k)|赞(0)|评价(0)|浏览(246)

本文整理了Java中java.util.concurrent.CancellationException.getCause()方法的一些代码示例,展示了CancellationException.getCause()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CancellationException.getCause()方法的具体详情如下:
包路径:java.util.concurrent.CancellationException
类名称:CancellationException
方法名:getCause

CancellationException.getCause介绍

暂无

代码示例

代码示例来源:origin: twitter/distributedlog

/**
 * Extract the exception (throwable) inside the ScheduledFutureTask
 * @param runnable - The runable that was executed
 * @return exception enclosed in the Runnable if any; null otherwise
 */
private Throwable extractThrowable(Runnable runnable) {
  // Check for exceptions wrapped by FutureTask.
  // We do this by calling get(), which will cause it to throw any saved exception.
  // Check for isDone to prevent blocking
  if ((runnable instanceof Future<?>) && ((Future<?>) runnable).isDone()) {
    try {
      ((Future<?>) runnable).get();
    } catch (CancellationException e) {
      LOG.info("Task {} cancelled", runnable, e.getCause());
    } catch (InterruptedException e) {
      LOG.info("Task {} was interrupted", runnable, e);
    } catch (ExecutionException e) {
      return e.getCause();
    }
  }
  return null;
}

代码示例来源:origin: google/guava

public void testCancel_notDoneNoInterrupt() throws Exception {
 Future<?> future = newFutureInstance();
 assertTrue(future.cancel(false));
 assertTrue(future.isCancelled());
 assertTrue(future.isDone());
 assertNull(tryInternalFastPathGetFailure(future));
 try {
  future.get();
  fail("Expected CancellationException");
 } catch (CancellationException e) {
  assertNotNull(e.getCause());
 }
}

代码示例来源:origin: google/guava

public void testCancel_notDoneInterrupt() throws Exception {
 Future<?> future = newFutureInstance();
 assertTrue(future.cancel(true));
 assertTrue(future.isCancelled());
 assertTrue(future.isDone());
 assertNull(tryInternalFastPathGetFailure(future));
 try {
  future.get();
  fail("Expected CancellationException");
 } catch (CancellationException e) {
  assertNotNull(e.getCause());
 }
}

代码示例来源:origin: google/guava

@GwtIncompatible
public void testImmediateCancelledFutureStack() throws Exception {
 ListenableFuture<String> future = CallerClass1.makeImmediateCancelledFuture();
 assertTrue(future.isCancelled());
 try {
  CallerClass2.get(future);
  fail();
 } catch (CancellationException expected) {
  // There should be two CancellationException chained together.  The outer one should have the
  // stack trace of where the get() call was made, and the inner should have the stack trace of
  // where the immediateCancelledFuture() call was made.
  List<StackTraceElement> stackTrace = ImmutableList.copyOf(expected.getStackTrace());
  assertFalse(Iterables.any(stackTrace, hasClassName(CallerClass1.class)));
  assertTrue(Iterables.any(stackTrace, hasClassName(CallerClass2.class)));
  // See AbstractFutureCancellationCauseTest for how to set causes.
  assertThat(expected.getCause()).isNull();
 }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testCancel_notDoneNoInterrupt() throws Exception {
 Future<?> future = newFutureInstance();
 assertTrue(future.cancel(false));
 assertTrue(future.isCancelled());
 assertTrue(future.isDone());
 try {
  future.get();
  fail("Expected CancellationException");
 } catch (CancellationException e) {
  assertNotNull(e.getCause());
 }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testCancel_notDoneInterrupt() throws Exception {
 Future<?> future = newFutureInstance();
 assertTrue(future.cancel(true));
 assertTrue(future.isCancelled());
 assertTrue(future.isDone());
 try {
  future.get();
  fail("Expected CancellationException");
 } catch (CancellationException e) {
  assertNotNull(e.getCause());
 }
}

代码示例来源:origin: threadly/threadly

@Test
 public void withRunningStackTest() throws InterruptedException, ExecutionException {
  SettableListenableFuture<Object> slf = new SettableListenableFuture<>();
  slf.setRunningThread(Thread.currentThread());
  CancelDebuggingListenableFuture<Object> debugFuture = new CancelDebuggingListenableFuture<>(slf);
  
  assertTrue(debugFuture.cancel(false));
  
  try {
   debugFuture.get();
   fail("Exception should have thrown");
  } catch (CancellationException e) {
   // expected
   assertNotNull(e.getCause());
   assertTrue(e.getCause() instanceof FutureProcessingStack);
   assertEquals(this.getClass().getName(), e.getCause().getStackTrace()[3].getClassName());
  }
 }
}

代码示例来源:origin: com.google.guava/guava-tests

@GwtIncompatible
public void testImmediateCancelledFutureStack() throws Exception {
 ListenableFuture<String> future = CallerClass1.makeImmediateCancelledFuture();
 assertTrue(future.isCancelled());
 try {
  CallerClass2.get(future);
  fail();
 } catch (CancellationException expected) {
  // There should be two CancellationException chained together.  The outer one should have the
  // stack trace of where the get() call was made, and the inner should have the stack trace of
  // where the immediateCancelledFuture() call was made.
  List<StackTraceElement> stackTrace = ImmutableList.copyOf(expected.getStackTrace());
  assertFalse(Iterables.any(stackTrace, hasClassName(CallerClass1.class)));
  assertTrue(Iterables.any(stackTrace, hasClassName(CallerClass2.class)));
  // See AbstractFutureCancellationCauseTest for how to set causes.
  assertThat(expected.getCause()).isNull();
 }
}

代码示例来源:origin: threadly/threadly

@Test
public void notStartedTest() throws InterruptedException, ExecutionException {
 SettableListenableFuture<Object> slf = new SettableListenableFuture<>();
 CancelDebuggingListenableFuture<Object> debugFuture = new CancelDebuggingListenableFuture<>(slf);
 
 assertTrue(debugFuture.cancel(false));
 
 try {
  debugFuture.get();
  fail("Exception should have thrown");
 } catch (CancellationException e) {
  // expected
  assertNull(e.getCause());
 }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testCancel_notDoneInterrupt() throws Exception {
 InterruptibleFuture future = new InterruptibleFuture();
 assertTrue(future.cancel(true));
 assertTrue(future.isCancelled());
 assertTrue(future.isDone());
 assertTrue(future.wasInterrupted());
 assertTrue(future.interruptTaskWasCalled);
 try {
  future.get();
  fail("Expected CancellationException");
 } catch (CancellationException e) {
  // See AbstractFutureCancellationCauseTest for how to set causes
  assertNull(e.getCause());
 }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testCancel_notDoneNoInterrupt() throws Exception {
 InterruptibleFuture future = new InterruptibleFuture();
 assertTrue(future.cancel(false));
 assertTrue(future.isCancelled());
 assertTrue(future.isDone());
 assertFalse(future.wasInterrupted());
 assertFalse(future.interruptTaskWasCalled);
 try {
  future.get();
  fail("Expected CancellationException");
 } catch (CancellationException e) {
  // See AbstractFutureCancellationCauseTest for how to set causes
  assertNull(e.getCause());
 }
}

相关文章