Java Code Examples for java.util.concurrent.FutureTask#isCancelled()

The following examples show how to use java.util.concurrent.FutureTask#isCancelled() . 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: DeletionService.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example 2
Source File: NonblockingScheduledExecutor.java    From bt with Apache License 2.0 6 votes vote down vote up
@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());
			}
		}
		
	}
}
 
Example 3
Source File: LoggingScheduledThreadPoolExecutor.java    From bt with Apache License 2.0 6 votes vote down vote up
@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);
		
	}
}
 
Example 4
Source File: DeletionService.java    From big-c with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example 5
Source File: NonblockingScheduledExecutor.java    From mldht with Mozilla Public License 2.0 6 votes vote down vote up
@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());
			}
		}
		
	}
}
 
Example 6
Source File: LoggingScheduledThreadPoolExecutor.java    From mldht with Mozilla Public License 2.0 6 votes vote down vote up
@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);
		
	}
}
 
Example 7
Source File: ReformatAndOptimizeImportsProcessor.java    From idea-android-studio-plugin with GNU General Public License v2.0 6 votes vote down vote up
@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);
            }
        }
    });
}
 
Example 8
Source File: AbstractLayoutCodeProcessor.java    From consulo with Apache License 2.0 6 votes vote down vote up
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;
    }
  });
}
 
Example 9
Source File: AThreadPool.java    From ans-android-sdk with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 高优先级同步执行:主要时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;
}
 
Example 10
Source File: FutureExecution.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@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);
        }
    };
}
 
Example 11
Source File: FlowEngine.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 12
Source File: FlowEngine.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 13
Source File: BeakerCellExecutor.java    From beakerx with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: AbstractLayoutCodeProcessor.java    From consulo with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 15
Source File: Customized.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static <V> void run(FutureTask<V> task) {
    boolean isCancelled = task.isCancelled();
    task.run();
    check(task.isDone());
    equal(isCancelled, task.isCancelled());
}