Runnable是执行工作的独立任务,但是它不返回任何值。在Java SE5中引入的Callable是一种具有类型参数的泛型,它的类型参数表的是从方法call()中返回的值,并且必须使用ExecutorServices.submit()方法调用它,下面是一个简单示例。

 

package com.test;  
2.  
3.import java.util.ArrayList;  
4.import java.util.List;  
5.import java.util.concurrent.Callable;  
6.import java.util.concurrent.ExecutionException;  
7.import java.util.concurrent.ExecutorService;  
8.import java.util.concurrent.Executors;  
9.import java.util.concurrent.Future;  
10.  
11.public class CallableTest {  
12.    public static void main(String[] args) {  
13.        ExecutorService exec=Executors.newCachedThreadPool();  
14.        List<Future<String>> results=new ArrayList<Future<String>>();  
15.          
16.        for(int i=0;i<5;i++) {  
17.            results.add(exec.submit(new TaskWithResult(i)));  
18.        }  
19.          
20.        for(Future<String> fs :results) {  
21.            try {  
22.                System.out.println(fs.get());  
23.            } catch (InterruptedException e) {  
24.                e.printStackTrace();  
25.            } catch (ExecutionException e) {  
26.                e.printStackTrace();  
27.            }  
28.        }  
29.    }  
30.}  
31.  
32.class TaskWithResult implements Callable<String> {  
33.    private int id;  
34.    public TaskWithResult(int id) {  
35.        this.id=id;  
36.    }  
37.      
38.    @Override  
39.    public String call() throws Exception {  
40.        return "result of TaskWithResult "+id;  
41.    }  
42.}  
package com.test;

import java.util.ArrayList;
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 CallableTest {
    public static void main(String[] args) {
        ExecutorService exec=Executors.newCachedThreadPool();
        List<Future<String>> results=new ArrayList<Future<String>>();
        
        for(int i=0;i<5;i++) {
            results.add(exec.submit(new TaskWithResult(i)));
        }
        
        for(Future<String> fs :results) {
            try {
                System.out.println(fs.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
}

class TaskWithResult implements Callable<String> {
    private int id;
    public TaskWithResult(int id) {
        this.id=id;
    }
    
    @Override
    public String call() throws Exception {
        return "result of TaskWithResult "+id;
    }
}

 

实验结果:

 

result of TaskWithResult 0 result of TaskWithResult 1 result of TaskWithResult 2 result of TaskWithResult 3 result of TaskWithResult 4

submit()方法回产生Future对象,它用Callable返回结果的特定类型进行了参数化。可以用isDone()方法来查询Future是否已经完成,当任务完成时,它具有一个结果,可以调用get()方法获取该结果。也可以不用isDone()进行检查就直接调用get(),在这种情况下,get()将阻塞,直至结果准备就绪。还可以在试图调用get()来获取结果之前,先调用具有超时的get(),或者调用isDone()来查看任务是否完成。