示例部分代码:
String param = "hello"
//举例用的线程池,一般建议自定义线程池
ExecutorService executorService = Executors.newFixedThreadPool(5);
CompletionService<Object> completionService = new ExecutorCompletionService<>(executorService);
completionService.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
// 模拟耗时任务
return simulationService.get(param);
}
});
Object res= Future<Integer> future = completionService.take().get();
// 关闭线程池
executorService.shutdown();