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
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
推荐阅读
-
springboot整合shiro多验证登录功能的实现(账号密码登录和使用手机验证码登录)
-
Oracle密码文件的使用和维护第1/3页
-
Android使用AudioRecord和AudioTrack完成音频的采集和播放以及使用MediaCodec完成硬编和硬解
-
Android获取当前应用分配的最大内存和目前使用内存的方法
-
PHP获取ip对应地区和使用网络类型的方法,phpip
-
python使用range函数计算一组数和的方法
-
Eclipse中关于mybatis插件geneartor的使用和遇到的一些问题
-
DevExpress的GridControl的使用以及怎样添加列和绑定数据源
-
PHP获取ip对应地区和使用网络类型的方法
-
正确安装显卡和使用显卡的经验小结