package com.cdp.ecosaas.portal.web.rest.fuDou;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.concurrent.*;
public class FutureTest{
public static void main(String[] args) {
Callable<List<Integer>> task = new Callable<List<Integer>>() {
@Override
public List<Integer> call() throws Exception {
List<Integer> objects = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
System.out.println("i = " + i);
objects.add(i);
}
return objects;
}
};
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
Future<List<Integer>> future = executorService.submit(task);
List<Integer> lisyt = future.get(2000 * 1, TimeUnit.MILLISECONDS);
System.out.println("任务成功返回:" + lisyt.toString());
} catch (TimeoutException ex) {
System.out.println("处理超时啦....");
ex.printStackTrace();
} catch (Exception e) {
System.out.println("处理失败.");
e.printStackTrace();
} finally {
System.out.println("关闭线程池.");
executorService.shutdown();
}
}
}