当前位置:网站首页>@Async does not execute asynchronously
@Async does not execute asynchronously
2022-07-24 05:44:00 【My benefits remain the same】
Cause of failure
[email protected] The startup class does not add @EnableAsync annotation .
2. Asynchronous methods use annotations @Async The return value of can only be void perhaps Future.
3. Didn't go Spring Proxy class . because @Transactional and @Async The implementation of annotations is based on Spring Of AOP, and AOP The implementation of is based on the dynamic agent pattern . So the reason why annotations fail is obvious , It may be because the method is called by the object itself rather than the proxy object , Because it didn't go through Spring Container management .
Problem code :
A method of this class is called in a class , No proxy object .
@PostConstruct
public void test(){
Long a = System.currentTimeMillis();
try {
test1();
test2();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(">>>>>>>"+(System.currentTimeMillis()-a));
}
@Async("")
public void test1() throws InterruptedException {
System.out.println(1);
Thread.sleep(10000);
System.out.println(2);
}
@Async("")
public void test2() throws InterruptedException {
System.out.println(3);
Thread.sleep(10000);
System.out.println(4);
}The result is
1->2->3->4
solve :
Add interface classes and implementation classes
public interface DataService {
public void test1();
public void test2();
}Implementation class :
@Service
public class DataServiceImpl implements DataService {
@Override
@Async("asyncServiceExecutor")
public void test1() {
System.out.println(1);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(2);
}
@Override
@Async("asyncServiceExecutor")
public void test2() {
System.out.println(3);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(4);
}
}call :
@Autowired
private DataService dataService;
@PostConstruct
public void test(){
Long a = System.currentTimeMillis();
dataService.test1();
dataService.test2();
System.out.println(">>>>>>>"+(System.currentTimeMillis()-a));
}The result is
1->3->2->4 perhaps 1->3->4->2
边栏推荐
猜你喜欢
随机推荐
Open Web3, once unpopular decentralized identity (did)
highcharts使用自定义矢量地图
Flink Watermark机制
mysqldump 导出中文乱码
Principle of fusdt liquidity pledge mining development logic system
WASM VS EVM,波卡的选择预示了公链未来
Substrate 技术及生态6月大事记 | Polkadot Decoded 圆满落幕,黑客松获胜项目为生态注入新生力量
多商户商城系统功能拆解10讲-平台端商品单位
多商户商城系统功能拆解08讲-平台端商品分类
MySQL 批量插入demo
【activiti】流程变量
Authorized access to MySQL database
去中心化的底层是共识——Polkadot 混合共识机制解读
Polkadot | 一文解读颠覆传统社媒的Liberty计划如何在波卡落地
Define attribute of UMI
【mycat】mycat相关概念
MySQL误操作后如何快速恢复数据
【activiti】activiti介绍
Cess test online line! The first decentralized storage network to provide multiple application scenarios
PoS机制随机性解读,波卡的随机性原理如何运作?









