I. Overview
In our environment, we often create asynchronous tasks, and our biggest idea is not to let time-consuming tasks block the operation of our methods.
Two. How to use
It's very easy to use asynchronous tasks in Springboot, we only need two steps to complete
[1] Open Async
[2] Defining Async methods
Let's take a look at one of our demo examples:
We just need to add an annotation that initiates an asynchronous task on our main startup class
@SpringBootApplication@EnableAsync Public class Springbootapplicationstarter { publicstaticvoid main (string[] args) { Springapplication.run (springbootapplicationstarter. class , args);} }
@Service Public classasynctask { @Async Public void Async() { Try{TimeUnit.SECONDS.sleep (3); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System. out. println ("Task Complete"); }}
On our business code, add a note of an asynchronous task to it.
020 Asynchronous tasks