高效开发:Java中Future的使用(Future代表的是异步执行的结果)
1、Future是java 1.5引入的一个interface,可以方便的用于异步结果的获取。
2、Future代表的是异步执行的结果,意思是当异步执行结束之后,返回的结果将会保存在Future中。
那么我们什么时候会用到Future呢? 一般来说,当我们执行一个长时间运行的任务时,使用Future就可以让我们暂时去处理其他的任务,等长任务执行完毕再返回其结果。
经常会使用到Future的场景有:1. 计算密集场景。2. 处理大数据量。3. 远程方法调用等,都是耗时比较长的需要等待的任务。
上代码例子:Controller层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import java.util.concurrent.Future; /** * 首页异步请求统计接口 * * @return */ @RequestMapping (value = "/getAssetAsyncComponent" , method = { RequestMethod.GET }) public BaseResult getAssetAsyncComponent() throws Exception { RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes(); long currentTimeMillis = System.currentTimeMillis(); Future<Map<String, Object>> countAssetInfoByAssetType = assetMgrAsyncComponentTask .countAssetInfoByAssetType(requestAttributes); Future<Map<String, Object>> getCountUnAsset = assetMgrAsyncComponentTask.getCountUnAsset(requestAttributes); Future<Map<String, Object>> getAssetFoundTaskTop = assetMgrAsyncComponentTask .getAssetFoundTaskTop(requestAttributes); Future<Map<String, Object>> listBusinessHomePage = assetMgrAsyncComponentTask .listBusinessHomePage(requestAttributes); Future<Map<String, Object>> getTop5Business = assetMgrAsyncComponentTask.getTop5Business(requestAttributes); for (;;) { if (countAssetInfoByAssetType.isDone() && getCountUnAsset.isDone() && getAssetFoundTaskTop.isDone() && listBusinessHomePage.isDone() && getTop5Business.isDone()) { // 5个任务都调用完成,退出循环等待 break ; } Thread.sleep( 300 ); } Map<String, Object> map = new HashMap<String, Object>(); map.put( "countAssetInfoByAssetType" , countAssetInfoByAssetType.get()); map.put( "getCountUnAsset" , getCountUnAsset.get()); map.put( "getAssetFoundTaskTop" , getAssetFoundTaskTop.get()); map.put( "listBusinessHomePage" , listBusinessHomePage.get()); map.put( "getTop5Business" , getTop5Business.get()); long currentTimeMillis1 = System.currentTimeMillis(); System.out.println( "首页 第一次请求的时候 异步获取全部的数据 task任务总耗时:" + (currentTimeMillis1 - currentTimeMillis) + "ms" ); return new BaseResult(map); } |
下一层加注解异步@Async即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import org.springframework.scheduling.annotation.AsyncResult; @Async public Future<Map<String, Object>> countAssetInfoByAssetType(RequestAttributes requestAttributes) { Map<String, Object> resultsMap = new HashMap<String, Object>(); long begin = System.currentTimeMillis(); try { RequestContextHolder.setRequestAttributes(requestAttributes, true ); BaseResult baseResult = assetInfoQueryService.countAssetInfoByAssetType(); resultsMap = (Map<String, Object>) baseResult.getData(); } catch (Exception e) { resultsMap.put( "error" , "countAssetInfoByAssetType 接口异常" ); } long end = System.currentTimeMillis(); System.out.println( "countAssetInfoByAssetType 接口 耗时(秒)=" + (end - begin) / 1000.0 ); return new AsyncResult<Map<String, Object>>(resultsMap); } |
也可以去看 https://blog.51cto.com/u_15287666/2976131 此博主的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具