java Future用法和意义一句话击破 [转]
package demo.future;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
 * 试验 Java 的 Future 用法
public class FutureTest {
    public static class Task implements Callable<String> {
        public String call() throws Exception {
            String tid = String.valueOf(Thread.currentThread().getId());
            System.out.printf("Thread#%s : in call\n", tid);
            return tid;
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        List<Future<String>> results = new ArrayList<Future<String>>();
        ExecutorService es = Executors.newCachedThreadPool();
        for(int i=0; i<100;i++)
            results.add(es.submit(new Task()));
        for(Future<String> res : results)

Leave a Reply

You must be logged in to post a comment.