FutureTask cancel - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for FutureTask cancel method.

This code example shows how to use the following methods:cancel
     * @see #isCancelled() 
     * @see #onCancelled(Object) 
     */ 
    public final boolean cancel(boolean mayInterruptIfRunning) {
        mCancelled.set(true);
        return mFuture.cancel(mayInterruptIfRunning);
    } 
 
    /** 
     * Waits if necessary for the computation to complete, and then retrieves 
     * its result. 
     *  
     * @return The computed result. 
     *  
     * @throws CancellationException 
     *             If the computation was cancelled. 
     * @throws ExecutionException 
     *             If the computation threw an exception. 
     * @throws InterruptedException 
     *             If the current thread was interrupted while waiting. 
5
CodeRank
This code example shows how to use the following methods:cancel, get, isDone
        }; 
        Executor exec = Executors.newSingleThreadExecutor(deamonThreadFactory);
         
        LOG.debug("Awaiting results");
        final FederatedQueryResultsClient resultsClient = fqpClient.query(query, delegationRef, null);
        FutureTask<Boolean> queryCompletionTask = new FutureTask<Boolean>(new Callable<Boolean>() {
            public Boolean call() throws RemoteException, InterruptedException {
                boolean complete = false;
                while (!complete) {
                    complete = resultsClient.isProcessingComplete();
                    Thread.sleep(500);
                } 
                return Boolean.valueOf(complete);
            } 
        }); 
        exec.execute(queryCompletionTask);
         
        LOG.debug("Checking for completion");
        Boolean complete = Boolean.FALSE;
        try { 

6
CodeRank
This code example shows how to use the following methods:sleep, cancel
	/** 
	 * @param args 
	 */ 
	public static void main(String[] args)
	{ 
		FutureTask<Void> task = new FutureTask<Void>(new Callable<Void>() {
 
			boolean keepRunning = true;
			 
			@Override 
			public Void call() throws Exception
			{ 
				return null; 
			} 
			 
			public void run() 
			{ 
				while (keepRunning)
				{ 
					System.out.println("gagaga");

6
CodeRank
This code example shows how to use the following methods:cancel
 
    public boolean cancel( boolean mayInterruptIfRunning ) {
        if( future==null )
            throw new UnsupportedOperationException("You cannot cancel this task before calling future()");
 
        return future.cancel(mayInterruptIfRunning);
    } 
     
    public boolean isCancelled(){ 
        return future == null ? false : future.isCancelled();
    } 
 
    /** 
     * @throws Exception, captured on passed to onException() if present. 
     */ 
    protected void onPreExecute() throws Exception {}
 
    /** 
     * @param t the result of {@link #call()} 
     * @throws Exception, captured on passed to onException() if present. 
5
CodeRank
This code example shows how to use the following methods:cancel, get
 
  @Override 
  public UnjoinCompleteEvent get(final long timeout, final TimeUnit unit) throws InterruptedException,
      ExecutionException, TimeoutException {
    try { 
      return _future.get(timeout, unit);
    } 
    catch (final TimeoutException e) {
      _future.cancel(true);
      throw e;
    } 
  } 
 
  @Override 
  public boolean cancel(final boolean mayInterruptIfRunning) {
    return _future.cancel(mayInterruptIfRunning);
  } 
 
  @Override 
  public boolean isCancelled() { 
5
CodeRank
This code example shows how to use the following methods:cancel
     * @see #isCancelled() 
     * @see #onCancelled(Object) 
     */ 
    public final boolean cancel(boolean mayInterruptIfRunning) {
        mCancelled.set(true);
        return mFuture.cancel(mayInterruptIfRunning);
    } 
 
    /** 
     * Waits if necessary for the computation to complete, and then retrieves 
     * its result. 
     *  
     * @return The computed result. 
     * @throws CancellationException If the computation was cancelled. 
     * @throws ExecutionException If the computation threw an exception. 
     * @throws InterruptedException If the current thread was interrupted while 
     *             waiting. 
     */ 
    public final Result get() throws InterruptedException, ExecutionException { 
        return mFuture.get();
3
CodeRank
This code example shows how to use the following methods:cancel
     * 
     * @see #isCancelled() 
     * @see #onCancelled(Object) 
     */ 
    public final boolean cancel(boolean mayInterruptIfRunning) {
        return mFuture.cancel(mayInterruptIfRunning);
    } 
 
    /** 
     * Waits if necessary for the computation to complete, and then 
     * retrieves its result. 
     * 
     * @return The computed result. 
     * 
     * @throws CancellationException If the computation was cancelled. 
     * @throws ExecutionException If the computation threw an exception. 
     * @throws InterruptedException If the current thread was interrupted 
     *         while waiting. 
     */ 
    public final Result get() throws InterruptedException, ExecutionException { 
3
CodeRank
This code example shows how to use the following methods:cancel
	 *  
	 * @see #isCancelled() 
	 * @see #onCancelled() 
	 */ 
	public final boolean cancel(boolean mayInterruptIfRunning) {
		return mFuture.cancel(mayInterruptIfRunning);
	} 
 
	/** 
	 * Waits if necessary for the computation to complete, and then retrieves 
	 * its result. 
	 *  
	 * @return The computed result. 
	 *  
	 * @throws CancellationException 
	 *             If the computation was cancelled. 
	 * @throws ExecutionException 
	 *             If the computation threw an exception. 
	 * @throws InterruptedException 
	 *             If the current thread was interrupted while waiting. 
3
CodeRank
This code example shows how to use the following methods:cancel
	public boolean cancel(boolean mayInterruptIfRunning)
	{ 
		if (future == null)
			throw new UnsupportedOperationException("You cannot cancel this task before calling future()");
 
		return future.cancel(mayInterruptIfRunning);
	} 
 
	/** 
	 * @throws Exception 
	 *             , captured on passed to onException() if present. 
	 */ 
	protected void onPreExecute() throws Exception 
	{ 
	} 
 
	/** 
	 * @param t 
	 *            the result of {@link #call()} 
	 * @throws Exception 
3
CodeRank
This code example shows how to use the following methods:cancel
	 */ 
	public void cancelExecution(final boolean forceInterruption) {
		final SubmittedTaskState estadoAnterior = currentState;
		final SubmittedTaskState estadoCancelado = estadoAnterior.getStateWhenCancelled();
		currentState = estadoCancelado;
		this.ownFuture.cancel(forceInterruption);
 
		// Si cambiamos al estado cancelado, notificamos 
		if (SubmittedTaskState.CANCELLED.equals(estadoCancelado) && !estadoCancelado.equals(estadoAnterior)) {
			notifyListenerCancelledTask(); 
		} 
	} 
 
	/** 
	 * Notifica al listener del procesor si existe alguno que la tarea se completó 
	 */ 
	private void notifyListenerCancelledTask() { 
		if (listener == null) {
			return; 
		} 
3
CodeRank
cancel Method Overview
Attempts to cancel execution of this task.
See Code Examples for other Java 8 FutureTask Methods: