Code example for FutureTask

Methods: cancel, get

0
	 */ 
	private boolean invalidateAndRefreshAuthToken() { 
		Log.d(LOG_TAG, "Retrying due to auth failure");
		// This FutureTask doesn't do anything -- it exists simply to be 
		// blocked upon using get(). 
		final FutureTask<?> whenFinishedFuture = new FutureTask<Object>(
				new Runnable() {
					public void run() { 
					} 
				}, null); 
 
		final AtomicBoolean finalSuccess = new AtomicBoolean(false);
		auth.invalidateAndRefresh(new AuthCallback() {
			// @Override 
			public void onAuthResult(boolean success) {
				finalSuccess.set(success);
				whenFinishedFuture.run();
			} 
		}); 
 
		try { 
			Log.d(LOG_TAG, "waiting for invalidate");
			whenFinishedFuture.get(AUTH_TOKEN_INVALIDATE_REFRESH_TIMEOUT,
					TimeUnit.MILLISECONDS);
			Log.d(LOG_TAG, "invalidate finished");
			return true; 
 
		} catch (InterruptedException e) {
			Log.e(LOG_TAG, "Failed to invalidate", e);
		} catch (ExecutionException e) {
			Log.e(LOG_TAG, "Failed to invalidate", e);
		} catch (TimeoutException e) {
			Log.e(LOG_TAG, "Invalidate didn't complete in time", e);
		} finally { 
			whenFinishedFuture.cancel(false);
		} 
 
		return false; 
	}