Converting Asynchronous Calls to Synchronous Execution with Spring @Async for Server‑Side Interface Performance Optimization
This article explains how to improve the performance of multi‑query server interfaces by converting asynchronous calls to synchronous execution using Spring's @Async annotation and CountDownLatch, outlining suitable scenarios, providing Java pseudocode, and sharing practical implementation tips.
Previously I wrote an article sharing a bug I encountered at work caused by a performance optimization: Asynchronous Query Converted to Synchronous with Redis Business Implementation Bug Share .
Recently I faced a similar task: several multi‑query APIs are ideal candidates for this asynchronous‑to‑synchronous optimization, so I am sharing the scheme used in server‑side interface performance tuning.
In my opinion, a query interface suitable for this scheme (writes are handled separately) has the following characteristics:
Multiple queries
Each query takes a relatively long time
Queries are independent of each other's results
Pseudocode:
@Override
public void doExecute(Map<String, Object> dataMap) {
doSomething(dataMap);
CountDownLatch countDownLatch = new CountDownLatch(3);
teacherPadAsyncService.do1(dataMap, countDownLatch, params);
teacherPadAsyncService.do2(dataMap, countDownLatch, params);
teacherPadAsyncService.do3(dataMap, countDownLatch, params);
try {
countDownLatch.await();
} catch (InterruptedException e) {
logger.error("异步处理线程异常", e);
}
}The implementation is straightforward: use Spring's @Async annotation. Some configuration adjustments are required (not detailed here), and thread‑safety must be considered.
@Async
public void do1(Map<String, Object> dataMap, CountDownLatch countDownLatch, Params params) {
try {
dosomething(dataMap, params);
} catch (Exception e) {
dosomething();
} finally {
countDownLatch.countDown();
}
}This is a conventional optimization approach; I will share other schemes when the opportunity arises.
Disclaimer: The article was first published on the public account “FunTester”. Reproduction or publication by third parties (except Tencent Cloud) is prohibited.
Technical Articles Selected
Chinese Localization of Netdata Linux Performance Monitoring Tool
Performance Testing Framework, Third Edition
Enjoyable Performance Testing on the Linux Command Line
Illustrated HTTP Mind Map
Automatically Convert Swagger Docs into Test Code
Selenium 4.0 Alpha Release Notes
Selenium 4.0 Alpha Practical Guide
Unified Approach to Functional, Automated, and Performance Test Cases for API Testing
Non‑Technical Articles Selected
Programming Mindset for Everyone
Seven Skills to Become an Automation Test Engineer
Common Reasons for Web‑UI Automation Test Failures
Excuses Frequently Used by Testers
API Testing Basics
API Automation Testing Guide
The Future of QA Engineers
JSON Basics
2020 Tester Self‑Improvement Guide
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
