下面列出了java.util.concurrent.FutureTask#isCancelled ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void afterExecute(Runnable task, Throwable exception) {
if (task instanceof FutureTask<?>) {
FutureTask<?> futureTask = (FutureTask<?>) task;
if (!futureTask.isCancelled()) {
try {
futureTask.get();
} catch (ExecutionException ee) {
exception = ee.getCause();
} catch (InterruptedException ie) {
exception = ie;
}
}
}
if (exception != null) {
LOG.error("Exception during execution of task in DeletionService",
exception);
}
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if(exceptionHandler != null && r instanceof FutureTask<?>) {
FutureTask<?> ft = (FutureTask<?>) r;
if(ft.isDone() && !ft.isCancelled()) {
try {
ft.get();
} catch (InterruptedException | ExecutionException e) {
exceptionHandler.uncaughtException(null, e.getCause());
}
}
}
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if(r instanceof FutureTask<?>) {
FutureTask<?> ft = (FutureTask<?>) r;
if(ft.isDone() && !ft.isCancelled()) {
try {
ft.get();
} catch (InterruptedException | ExecutionException e) {
exceptionHandler.accept(e.getCause());
}
}
if(t != null)
exceptionHandler.accept(t);
}
}
@Override
protected void afterExecute(Runnable task, Throwable exception) {
if (task instanceof FutureTask<?>) {
FutureTask<?> futureTask = (FutureTask<?>) task;
if (!futureTask.isCancelled()) {
try {
futureTask.get();
} catch (ExecutionException ee) {
exception = ee.getCause();
} catch (InterruptedException ie) {
exception = ie;
}
}
}
if (exception != null) {
LOG.error("Exception during execution of task in DeletionService",
exception);
}
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if(exceptionHandler != null && r instanceof FutureTask<?>) {
FutureTask<?> ft = (FutureTask<?>) r;
if(ft.isDone() && !ft.isCancelled()) {
try {
ft.get();
} catch (InterruptedException | ExecutionException e) {
exceptionHandler.uncaughtException(null, e.getCause());
}
}
}
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if(r instanceof FutureTask<?>) {
FutureTask<?> ft = (FutureTask<?>) r;
if(ft.isDone() && !ft.isCancelled()) {
try {
ft.get();
} catch (InterruptedException | ExecutionException e) {
exceptionHandler.accept(e.getCause());
}
}
if(t != null)
exceptionHandler.accept(t);
}
}
@Override
@NotNull
public FutureTask<Boolean> preprocessFile(@NotNull PsiFile file, boolean processChangedTextOnly) throws IncorrectOperationException {
final FutureTask<Boolean> reformatTask = myReformatCodeProcessor.preprocessFile(file, processChangedTextOnly);
final FutureTask<Boolean> optimizeImportsTask = myOptimizeImportsProcessor.preprocessFile(file, false);
return new FutureTask<Boolean>(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
reformatTask.run();
if (!reformatTask.get() || reformatTask.isCancelled()) {
return false;
}
CodeStyleManagerImpl.setSequentialProcessingAllowed(false);
try {
optimizeImportsTask.run();
return optimizeImportsTask.get() && !optimizeImportsTask.isCancelled();
}
finally {
CodeStyleManagerImpl.setSequentialProcessingAllowed(true);
}
}
});
}
public FutureTask<Boolean> preprocessFile(@Nonnull PsiFile file, boolean processChangedTextOnly) throws IncorrectOperationException {
final FutureTask<Boolean> previousTask = getPreviousProcessorTask(file, processChangedTextOnly);
final FutureTask<Boolean> currentTask = prepareTask(file, processChangedTextOnly);
return new FutureTask<>(() -> {
try {
if (previousTask != null) {
previousTask.run();
if (!previousTask.get() || previousTask.isCancelled()) return false;
}
ApplicationManager.getApplication().runWriteAction(currentTask);
return currentTask.get() && !currentTask.isCancelled();
}
catch (ExecutionException e) {
ExceptionUtil.rethrowUnchecked(e.getCause());
throw e;
}
});
}
/**
* 高优先级同步执行:主要时get类对外操作接口
*
* @param callable
* @return
*/
public static Object syncHighPriorityExecutor(Callable callable) {
Object object = null;
FutureTask<Object> futureTask = new FutureTask<Object>(callable);
highService.execute(futureTask);
while (!futureTask.isDone() && !futureTask.isCancelled()) {
try {
object = futureTask.get();
} catch (Throwable ignore) {
ExceptionUtil.exceptionThrow(ignore);
}
}
return object;
}
@Override
public Future<V> apply(InvocationContext<Future<V>> ctx) {
FutureTask<Future<V>> task = new NamedFutureTask<>("FutureExecution", () -> delegate.apply(ctx));
executor.execute(task);
return new Future<V>() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
ctx.fireEvent(CancellationEvent.INSTANCE);
return task.cancel(mayInterruptIfRunning);
}
@Override
public boolean isCancelled() {
return task.isCancelled();
}
@Override
public boolean isDone() {
return task.isDone();
}
@Override
public V get() throws InterruptedException, ExecutionException {
return task.get().get();
}
@Override
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
// at worst, the timeout here could possibly be 2x the requested value
return task.get(timeout, unit).get(timeout, unit);
}
};
}
/**
* Hook method called by the thread that executed the given runnable after execution of the runnable completed. Logs the fact of completion and any errors that might have occurred.
*
* @param runnable runnable
* @param throwable throwable
*/
@Override
protected void afterExecute(final Runnable runnable, final Throwable throwable) {
super.afterExecute(runnable, throwable);
if (runnable instanceof FutureTask<?>) {
final FutureTask<?> task = (FutureTask<?>) runnable;
try {
if (task.isDone()) {
if (task.isCancelled()) {
if (logger.isDebugEnabled()) {
logger.debug("A flow controller execution task '{}' has been cancelled.", runnable);
}
} else {
task.get(); //to raise any exceptions that might have occurred.
logger.debug("A Flow Controller execution task '{}' has completed.", runnable);
}
}
} catch (final CancellationException ce) {
if (logger.isDebugEnabled()) {
logger.debug("A flow controller execution task '{}' has been cancelled.", runnable);
}
} catch (final InterruptedException ie) {
if (logger.isDebugEnabled()) {
logger.debug("A flow controller execution task has been interrupted.", ie);
}
} catch (final ExecutionException ee) {
logger.error("A flow controller task execution stopped abnormally", ee);
}
} else {
logger.debug("A flow controller execution task '{}' has finished.", runnable);
}
}
/**
* Hook method called by the thread that executed the given runnable after execution of the runnable completed. Logs the fact of completion and any errors that might have occurred.
*
* @param runnable runnable
* @param throwable throwable
*/
@Override
protected void afterExecute(final Runnable runnable, final Throwable throwable) {
super.afterExecute(runnable, throwable);
if (runnable instanceof FutureTask<?>) {
final FutureTask<?> task = (FutureTask<?>) runnable;
try {
if (task.isDone()) {
if (task.isCancelled()) {
if (logger.isDebugEnabled()) {
logger.debug("A flow controller execution task '{}' has been cancelled.", runnable);
}
} else {
task.get(); //to raise any exceptions that might have occurred.
logger.debug("A Flow Controller execution task '{}' has completed.", runnable);
}
}
} catch (final CancellationException ce) {
if (logger.isDebugEnabled()) {
logger.debug("A flow controller execution task '{}' has been cancelled.", runnable);
}
} catch (final InterruptedException ie) {
if (logger.isDebugEnabled()) {
logger.debug("A flow controller execution task has been interrupted.", ie);
}
} catch (final ExecutionException ee) {
logger.error("A flow controller task execution stopped abnormally", ee);
}
} else {
logger.debug("A flow controller execution task '{}' has finished.", runnable);
}
}
private TryResult getResult(FutureTask<TryResult> ret) {
TryResult o;
try {
o = ret.get();
} catch (Exception e) {
e.printStackTrace();
return TryResult.createError(e.getMessage());
}
if (ret.isCancelled())
return TryResult.createError("Cancelled");
return o;
}
private void checkStop(FutureTask<Boolean> task, PsiFile file) {
try {
if (!task.get() || task.isCancelled()) {
myStopFormatting = true;
}
}
catch (InterruptedException | ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof IndexNotReadyException) {
LOG.warn(cause);
return;
}
LOG.error("Got unexpected exception during formatting " + file, e);
}
}
static <V> void run(FutureTask<V> task) {
boolean isCancelled = task.isCancelled();
task.run();
check(task.isDone());
equal(isCancelled, task.isCancelled());
}