先把longTimeMethod 封装到Spring的异步方法中,这个异步方法的返回值是Future的实例。这个方法一定要写在Spring管理的类中,注意注解@Async。

@Service
public class AsynchronousService{
@Async
public Future springAsynchronousMethod(){
Integer result = longTimeMethod();
return new AsyncResult(result);
}
}

其他类调用这个方法。这里注意,一定要其他的类,如果在同类中调用,是不生效的。

@Autowired
private AsynchronousService asynchronousService; public void useAsynchronousMethod(){
Future future = asynchronousService.springAsynchronousMethod();
future.get(1000, TimeUnit.MILLISECONDS);
}

其实Spring只不过在原生的Future中进行了一次封装,我们最终获得的还是Future实例。

最新文章

  1. Go语言开发第一个Hello,World
  2. android 命名 数组 所有国家 String[] COUNTRIES
  3. Error 2103 “Unhandled Error in Silverlight Application“ 解决办法
  4. 9月20日上午JavaScript函数
  5. [BTS]The join order has been enforced because a local join hint is used.;Duplicate key was ignored.".
  6. jenkins配置记录(2)--代码发布流程
  7. 学习之路三十二:VS调试的简单技巧
  8. Vue 模板
  9. java web 学习五(servlet开发1)
  10. linux下curl编程
  11. Internet Information Services安装与启动
  12. ACM学习之路__HDU 1045
  13. 实践作业2:黑盒测试实践——选择并下载测试工具 Day 2
  14. 多项式 之 快速傅里叶变换(FFT)/数论变换(NTT)/常用套路【入门】
  15. 第k小的元素
  16. Python3从零开始爬取今日头条的新闻【一、开发环境搭建】
  17. java项目打成war包
  18. QQ网页弹窗
  19. java 偏向锁,轻量锁,重量级锁
  20. Integer与int区别

热门文章

  1. 如何传递大文件(GB级别)
  2. MySQL Replication--复制基本工作原理
  3. 【CMDB】API传输验证
  4. VLAN实验5:利用三层交换机实现VLAN间路由
  5. jquery获取元素的display属性是不是none?
  6. 小程序~列表渲染~key
  7. ajax、axios、fetch 对比
  8. 项目Alpha冲刺(团队)-第十天冲刺
  9. 5、Python之包管理工具pip
  10. Spring框架:Controller和RestController区别