2014-07-25 42 views
4

標題幾乎總結了它。Java中可調用的是什麼?

我想知道可調用的概念和想法。我已經閱讀了關於callable和runnable之間差異的question here。但沒有人展示代碼,並給出了可調用的細節。我不想知道它們之間的區別。我想知道,

  1. 什麼是callable?

  2. 何時使用它們以及如何使用它們。

  3. 當他們在Android的行動 Android。

回答

4

您可以檢查此example

在這個例子中可調用任務返回線程的後一秒執行任務的名稱。我們使用Executor框架並行執行100個任務,並使用Future獲取提交任務的結果。

package com.journaldev.threads; 

import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutionException; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 

public class MyCallable implements Callable<String> { 

    @Override 
    public String call() throws Exception { 
     Thread.sleep(1000); 
     //return the thread name executing this callable task 
     return Thread.currentThread().getName(); 
    } 

    public static void main(String args[]){ 
     //Get ExecutorService from Executors utility class, thread pool size is 10 
     ExecutorService executor = Executors.newFixedThreadPool(10); 
     //create a list to hold the Future object associated with Callable 
     List<Future<String>> list = new ArrayList<Future<String>>(); 
     //Create MyCallable instance 
     Callable<String> callable = new MyCallable(); 
     for(int i=0; i< 100; i++){ 
      //submit Callable tasks to be executed by thread pool 
      Future<String> future = executor.submit(callable); 
      //add Future to the list, we can get return value using Future 
      list.add(future); 
     } 
     for(Future<String> fut : list){ 
      try { 
       //print the return value of Future, notice the output delay in console 
       // because Future.get() waits for task to get completed 
       System.out.println(new Date()+ "::"+fut.get()); 
      } catch (InterruptedException | ExecutionException e) { 
       e.printStackTrace(); 
      } 
     } 
     //shut down the executor service now 
     executor.shutdown(); 
    } 

} 

您還可以檢查Using Callable to Return Results From Runnables

+1

出色答卷! –

+0

當他們在Android上運行時 – Nepster

1

Callable類似於Runnable接口,但是它返回一個結果,並可能會拋出異常。 當您希望異步任務返回結果時使用它們。

異步計算的返回結果由Future表示。

您可以檢查實現這個簡單的例子使用FutureTask(實現RunnableFuture和未來)

public static void main(String[] args) { 

    // providing an anonymous callable to FutureTask 
    RunnableFuture<String> future = new FutureTask<String>(
      new Callable<String>() { 
       @Override 
       public String call() throws InterruptedException { 
        System.out.println("sleeping"); 
        Thread.sleep(2000); 
        System.out.println("returning"); 
        return "hello-world"; 
       } 

      }); 

    Thread t = new Thread(future); 
    t.start(); 

    try { 
     // the get Waits if necessary for the computation to complete 
     System.out.println(future.get()); 
    } catch (InterruptedException | ExecutionException e) { 
     e.printStackTrace(); 
    } 

} 
+0

在您運行未來任務的同一線程中等待什麼? –

+0

獲取調用會一直等到結果可用或中斷,取消或發生某些計算異常時。請注意,在調用get(main)時,處理仍然正在進行/或已經完成(在線程't'中)。 – Shail016

+0

@Jimmy Lunceford,你可以通過其他一些線程分享未來的參考資料,並打電話給你,但這裏指的是證明它的作用。即使在這裏,未來任務在線程't'中執行而不是'主要' – Shail016