`
taiwei.peng
  • 浏览: 228699 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Future和Callable的使用

阅读更多
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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics