package com.qd.thread.callable;import java.util.concurrent.Callable;/** * Created by chenlongbo on 2017/4/24. */public class TaskWithResult implements Callable{ private int id; public TaskWithResult(int id){ this.id = id; } public String call() throws Exception { return " result of TaskWithResult : "+id; }}
package com.qd.thread.callable;import java.util.ArrayList;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;/** * Created by chenlongbo on 2017/4/24. */public class CallableDemo { public static void main(String[] args) { ExecutorService exec = Executors.newCachedThreadPool(); ArrayList> futures = new ArrayList >(); for (int i = 0; i < 10; i++) { futures.add(exec.submit(new TaskWithResult(i))); } for(Future fs:futures){ try { System.out.println(fs.get()); } catch (InterruptedException e) { System.out.println(e); e.printStackTrace(); return; } catch (ExecutionException e) { System.out.println(e); e.printStackTrace(); }finally { exec.shutdown(); } } }}