Use of @enableasync and @async annotations in Springboot

Source: Internet
Author: User

Use of @enableasync and @async annotations in Springboot

The @Async is an asynchronous annotation, placed on a method, indicating that the thread calling the method executes asynchronously with this method and needs to be used in conjunction with @enableasync annotations.

1, first demonstration no @async, that is, there is no asynchronous execution of the situation

-Create a generic class countnumber and inject it into the IOC container

1 Package Com.example.demo;2 import Org.springframework.scheduling.annotation.Async;3 import org.springframework.stereotype.Component;4 5 @Component6  Public classCountNumber {7 8      Public voidPrintnumber () {9          for(intI=1; i<Ten; i++){TenSystem. out. println ("i ="+i); One         } A     } -}

-Get the IOC bean in spring boot class

1 Package Com.example.demo;2 import Java.util.concurrent.TimeUnit;3 import org.springframework.boot.SpringApplication;4 import org.springframework.boot.autoconfigure.SpringBootApplication;5 import Org.springframework.context.ConfigurableApplicationContext;6 import Org.springframework.context.annotation.ComponentScan;7 8 //@SpringBootApplication9 @ComponentScanTen  Public classspringboot3application { One  A      Public Static voidMain (string[] args) throws Exception { -  -Configurableapplicationcontext context = Springapplication.run (springboot3application.class, args); the  -Context.getbean (CountNumber.class). Printnumber (); -          for(intI=1; i<Ten; i++){ -System. out. println ("------------------"); +         } - context.close (); +     } A}

-Run output results:

12 3 456   789---------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----

As can be seen from the output, the startup class obtains the CountNumber object from the IOC container, then executes the object's Printnumber method, loops through the 9 numbers, and then proceeds back to the startup class after the execution of the method, and starts executing the FOR Loop statement. This is done sequentially from the whole process.

2, there is @async, that is, the situation of asynchronous execution

-Create a generic class countnumber and inject it into the IOC container and annotate the @async annotations on the methods of the class, indicating that the method is executed asynchronously.

1 Package Com.example.demo;2 import Org.springframework.scheduling.annotation.Async;3 import org.springframework.stereotype.Component;4 5 @Component6  Public classCountNumber {7 @Async8      Public voidPrintnumber () {9          for(intI=1; i<Ten; i++){TenSystem. out. println ("i ="+i); One         } A     } -}

-Get the beans in the IOC from the spring boot startup class, annotate the @enableasync annotations on the startup class, and start the @async asynchronous annotations. Or, only @springbootapplication annotations are annotated in the startup class because the above two annotations are already included in the annotation.

1 Package Com.example.demo;2 import Java.util.concurrent.TimeUnit;3 import org.springframework.boot.SpringApplication;4 import org.springframework.boot.autoconfigure.SpringBootApplication;5 import Org.springframework.context.ConfigurableApplicationContext;6 import Org.springframework.context.annotation.ComponentScan;7 import Org.springframework.scheduling.annotation.Async;8 import Org.springframework.scheduling.annotation.EnableAsync;9 Ten /*@SpringBootApplication annotations Achieve the same effect as @componentscan, @EnableAsync annotations*/ One //@SpringBootApplication A @ComponentScan - @EnableAsync -  Public classspringboot3application { the  -      Public Static voidMain (string[] args) throws Exception { -  -Configurableapplicationcontext context = Springapplication.run (springboot3application.class, args); +  -         /*@Async and @enableasync used together*/ +Context.getbean (CountNumber.class). Printnumber (); A          for(intI=1; i<Ten; i++){ atTimeUnit.MICROSECONDS.sleep (1); -System. out. println ("------------------"); -         } - context.close (); -     } -}

-Executes the startup class with the following output:

---------------------------------------------------------------------------------------------------------- --i  = 1  i  = 2  i  = 3  i  = 4  i  = 5  i  = 6  i  = 7  i  = 8  i  = 9 ------------------------------------------------------

As can be seen from the output, spring boot, after acquiring the CountNumber object in the IOC, continues execution down, executes the FOR Loop statement, and, on the other hand, executes the Printnumber method in the object after fetching the object. So the Printnumber method is executed asynchronously with the main thread.

Use of @enableasync and @async annotations in Springboot

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.