Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ Async for SpringBoot

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use SpringBoot @ Async", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use SpringBoot @ Async" this article.

SpringBoot uses @ Async to summarize

Note:

The @ Async will be invalidated in the following ways

1. Asynchronous methods are decorated with static

two。 Asynchronous classes do not use @ Component annotations (or other annotations) so that spring cannot scan to asynchronous classes

3. An asynchronous method cannot be in the same class as an asynchronous method

4. You need to use @ Autowired or @ Resource annotations in the class to inject automatically. You cannot manually new objects by yourself.

5. If you use the SpringBoot framework, you must add the @ EnableAsync annotation to the startup class

6. It is useless to mark @ Transactional on the Async method. It is valid to mark @ Transactional on the method called by the Async method.

SpringBoot implements Async interface 1. The startup class introduces the @ EnableAsync annotation @ SpringBootApplication @ EnableAsync public class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args);}} 2. Establish an asynchronous task class

We have built three asynchronous tasks with a delay of 1s and 2s respectively.

@ Componentpublic class AsyncTask {@ Async public void task1 () throws InterruptedException {long currentTimeMillis = System.currentTimeMillis (); Thread.sleep (1000); long currentTimeMillis1 = System.currentTimeMillis (); System.out.println ("task1 task time:" + (currentTimeMillis1-currentTimeMillis) + "ms");} @ Async public void task2 () throws InterruptedException {long currentTimeMillis = System.currentTimeMillis (); Thread.sleep (2000) Long currentTimeMillis1 = System.currentTimeMillis (); System.out.println ("task2 task time:" + (currentTimeMillis1-currentTimeMillis) + "ms");} @ Async public void task3 () throws InterruptedException {long currentTimeMillis = System.currentTimeMillis (); Thread.sleep (3000); long currentTimeMillis1 = System.currentTimeMillis (); System.out.println ("task3 task time:" + (currentTimeMillis1-currentTimeMillis) + "ms");}} 3. Set up the test interface @ RestController@RequestMapping ("/ test") public class TestController {@ Autowired private AsyncTask asyncTask; @ RequestMapping ("/ async") public String doTask () throws InterruptedException {long currentTimeMillis = System.currentTimeMillis (); asyncTask.task1 (); asyncTask.task2 (); asyncTask.task3 (); long currentTimeMillis1 = System.currentTimeMillis (); return "task task total time:" + (currentTimeMillis1-currentTimeMillis) + "ms" }}

Start the SpringBoot service and visit the / test/async interface. You can see that the task takes only 1 second.

Check the console and find that asynchronous task has also been successfully executed!

The above is all the content of the article "how to use SpringBoot @ Async". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report