FutureTask get - 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 get method.

This code example shows how to use the following methods:get
		assertEquals(1, getCorePoolSize(executor));
		assertEquals(Integer.MAX_VALUE, getMaxPoolSize(executor));
		assertEquals(Integer.MAX_VALUE, getQueueCapacity(executor));
		assertEquals(60, getKeepAliveSeconds(executor));
		assertEquals(false, getAllowCoreThreadTimeOut(executor));
		FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
			@Override 
			public String call() throws Exception {
				return "foo"; 
			} 
		}); 
		((ThreadPoolTaskExecutor)executor).execute(task);
		assertEquals("foo", task.get());
	} 
 
	@Test
	public void singleSize() { 
		Object executor = this.context.getBean("singleSize");
		assertEquals(42, getCorePoolSize(executor));
		assertEquals(42, getMaxPoolSize(executor));
7
CodeRank
This code example shows how to use the following methods:post, get
	 
	public static <T> T callInMainThread(Callable<T> call) throws Exception {
		if (isMainThread()) 
			return call.call();
		else { 
			FutureTask<T> task = new FutureTask<T>(call);
			getHandler().post(task);
			return task.get();
		} 
	} 
} 
8
CodeRank
This code example shows how to use the following methods:post, get, run
     * @param c The Callable to run 
     * @return The result of the callable 
     * @throws ExecutionException c's exception 
     */ 
    public static <T> T runOnUiThreadBlocking(Callable<T> c) throws ExecutionException {
        FutureTask<T> task = new FutureTask<T>(c);
        runOnUiThread(task);
        try { 
            return task.get();
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted waiting for callable", e);
        } 
    } 
 
    /** 
     * Run the supplied FutureTask on the main thread. The method will block only if the current 
     * thread is the main thread. 
     * 
     * @param task The FutureTask to run 
     * @return The queried task (to aid inline construction) 

6
CodeRank
This code example shows how to use the following methods:post, get
 
    public static <T> T callInMainThread(Callable<T> call) throws Exception {
        if (isMainThread()) { 
            return call.call();
        } else { 
            FutureTask<T> task = new FutureTask<T>(call);
            getHandler().post(task);
            return task.get();
        } 
    } 
 
    public static Bitmap takeScreenshot(View view, Bitmap.Config config) {
        int width = view.getWidth();
        int height = view.getHeight();
 
        if (view != null && width > 0 && height > 0) {
            Bitmap bitmap = Bitmap.createBitmap(width, height, config);
            Canvas canvas = new Canvas(bitmap);
            view.draw(canvas);
            return bitmap;
6
CodeRank
This code example shows how to use the following methods:get
    final Thread retrieverThread;
 
    if (robot != null) {
      return robot;
    } 
    robotRetriever = new FutureTask<Robot>(new Retriever());
    log.info("Creating Robot");
    retrieverThread = new Thread(robotRetriever, "robotRetriever");  // Thread safety reviewed
    retrieverThread.start();
    robot = robotRetriever.get(10, TimeUnit.SECONDS);
 
    return robot;
  } 
 
} 
7
CodeRank
This code example shows how to use the following methods:post, get, run
     * @param c The Callable to run 
     * @return The result of the callable 
     * @throws ExecutionException c's exception 
     */ 
    public static <T> T runOnUiThreadBlocking(Callable<T> c) throws ExecutionException {
        FutureTask<T> task = new FutureTask<T>(c);
        runOnUiThread(task);
        try { 
            return task.get();
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted waiting for callable", e);
        } 
    } 
 
    /** 
     * Run the supplied FutureTask on the main thread. The method will block 
     * only if the current thread is the main thread. 
     * 
     * @param task The FutureTask to run 
     * @return The queried task (to aid inline construction) 

6
CodeRank
This code example shows how to use the following methods:runOnMainSync, get
     * 
     * @param <R> Type of result to return 
     */ 
    public <R> R runTestOnUiThreadAndGetResult(Callable<R> callable)
            throws Exception { 
        FutureTask<R> task = new FutureTask<R>(callable);
        getInstrumentation().waitForIdleSync();
        getInstrumentation().runOnMainSync(task);
        return task.get();
    } 
 
    /** 
     * Loads url on the UI thread and blocks until onPageFinished is called. 
     */ 
    protected void loadUrlSync(final WebView webview,
                               CallbackHelper onPageFinishedHelper,
                               final String url) throws Exception {
        int currentCallCount = onPageFinishedHelper.getCallCount();
        loadUrlAsync(webview, url);
        onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
6
CodeRank
This code example shows how to use the following methods:get, isCancelled, isDone
        super.afterExecute(r, t);
         
        if (r instanceof FutureTask) {
            FutureTask task = (FutureTask)r;
            try { 
                if (task.isDone() && !task.isCancelled()) {
                    task.get();
                } 
            } catch (InterruptedException e) {
                // do nothing here. let code which is interested  
                // in the result of the task deal with this 
            } catch (ExecutionException e) {
                Throwable badStuff = e.getCause();
                if (badStuff instanceof RuntimeException || badStuff instanceof Error) {
                    Thread currThread = Thread.currentThread();
                    Thread.UncaughtExceptionHandler handler = currThread.getUncaughtExceptionHandler();
                     
                    // should not be null 
                    if (handler != null) {
                        handler.uncaughtException(currThread, badStuff);    
6
CodeRank
This code example shows how to use the following methods:get
     * <p> 
     * Waits for an idle sync on the main thread (see {@link Instrumentation#waitForIdle(Runnable)}) 
     * before executing this callable. 
     */ 
    public <T> T runOnUiThreadAndGetTheResult(Callable<T> callable) throws Throwable {
        FutureTask<T> future = new FutureTask<T>(callable);
        mInstrumentation.waitForIdle(future);
        try { 
            return future.get();
        } catch (ExecutionException e) {
            // Unwrap the cause of the exception and re-throw it. 
            throw e.getCause();
        } 
    } 
 
    /** 
     * Wake up the screen, useful in tests that want or need the screen to be on. 
     * <p> 
     * This is usually called from setUp() for tests that require it.  After calling this method, 
     * {@link #releaseScreenWakeLock()} must be called, this is usually done from tearDown(). 
6
CodeRank
This code example shows how to use the following methods:get
            @Override 
            public MediaPlayer call() throws Exception {
                return new MediaPlayer();
            } 
        }; 
        FutureTask<MediaPlayer> future = new FutureTask<MediaPlayer>(callable);
        getInstrumentation().runOnMainSync(future);
        return DynamicProxy.dynamicProxy(MediaPlayerProxy.class, future.get(1, TimeUnit.SECONDS));
    } 
} 
6
CodeRank
get Method Overview
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
See Code Examples for other Java 8 FutureTask Methods: