Solve the problem of slow SpringBoot query efficiency - asynchronous processing

There may be many asynchronous methods, and I will only give an example of a simple and practical method here.

Steps and code examples:

1. First write a new entity class, write methods in the entity class and complete the operations required by the business itself.

Add the @Component annotation to the entity class, and add the @Async annotation to the method; async is used to achieve asynchronous effects.

2. Create an entity class object where you need to call this method or automatically inject the entity class written above, and then call the asynchronous method.

 3. Turn on the asynchronous function at the launcher, if you do not add asynchronous function, it will not take effect

 After the addition is complete, asynchrony will be implemented. When a large amount of unrelated data needs to be queried at the same time, this method is very useful and easier to operate than threads.

Reminder: Asynchronous methods need to be stored separately from the caller, otherwise they will not take effect.

Tip: If what you want to do is not background automatic processing, you need to wait for the program to complete, then read on.

When you need to wait for the end of asynchronous concurrency, add return parameters like this:

Then use isDone to judge whether the asynchrony is over, true means it has ended.

 

 Write an infinite loop through while, jump out of the loop when true, and complete the asynchronous operation waiting.

Guess you like

Origin blog.csdn.net/guo0208/article/details/127089773