欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Future和Callable的使用

程序员文章站 2022-04-03 12:30:34
...
ExecutorService中的submit(Callable)方法可以执行Callable任务,并且使用Future接口中的get()方法获取返回值。示例如下:
public class HomeRegNumCallable implements Callable<Map<String, Object>>{

private String newDate;
private String oldTime;
private HomePageMapper homePageMapper;

public HomeRegNumCallable(String newDate,String oldTime,HomePageMapper homePageMapper) {
        this.newDate= newDate;
        this.oldTime=oldTime;
        this.homePageMapper=homePageMapper;
    }

@Override
public Map<String, Object> call() throws Exception {
return homePageMapper.queryRegNum(newDate,oldTime);
}
}

使用的地方
List<Map<String,Object>> mapList=new ArrayList<Map<String,Object>>();
ExecutorService executorService = Executors.newFixedThreadPool(2);
CompletionService completionService = new ExecutorCompletionService(executorService);
Future<Map<String, Object>> regNumFuture=completionService.submit(new HomeRegNumCallable(newDate,oldTime,homePageMapper));
mapList.add(regNumFuture.get());

https://blog.csdn.net/hongguo_cheng/article/details/78235180
相关标签: Future Callable