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