java多线程异步处理并获取处理后的返回值

示例部分代码:

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

你可能感兴趣的:(java技术,java)