import java.util.Date;
import java.util.concurrent.Callable;
public class MyThread implements Callable {
@Override
public Date call() throws Exception {
Date date = new Date();
return date;
}
}
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestFuture {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executorService = Executors.newFixedThreadPool(10);
ArrayList> result = new ArrayList>();
for( int i=0;i<10;i++) {
MyThread thread = new MyThread();
Future submit = executorService.submit(thread);
result.add(submit);
Thread.sleep(1000);
}
for(Future future : result) {
System.out.println(future.get().toString());
}
executorService.shutdown();
}
}
大概10秒后打印出结果