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

Case Analysis of java Asynchronous Task

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "java asynchronous task case analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "java Asynchronous Task example Analysis".

What is an asynchronous task

Whether in life or in a program, it can be divided into two types: synchronous and asynchronous.

Synchronization: for example, when you go to eat Haidilao, you have to order the bottom of the pot first, then order the food, then the waiter will serve the dish, and then you can eat it at last. This process has to be done in order.

Asynchronous task: or to eat Haidilao, eat a lot of people, there are a lot of people in front of you, you may have to wait in line, wait until you can enter the restaurant. But if you want to go to the bathroom halfway, what to do? you'll have to wait in line again when you come back. So there is a call number, you wait in line to get the number, and then you can press a massage, watch a movie, make a spa, and buy a cup of milk tea. . Finally, it's your turn. I'll let you know that the queue is here, and then you can go in. This process is asynchronous.

2. SpringBoot + Async

At first, I thought of opening a thread pool and leaving the task in the thread pool to finish.

Later, I remembered that SpringBoot has a more convenient asynchronous framework, Async.

The code is also very simple, just add @ Async to the method that needs to be executed asynchronously, and @ EnableAsync to the SpringBoot startup class

@ Async public void task () {/ / do something} III. Tread pit diary

The code is small, but the pit does not decrease as the amount of code decreases.

For convenience, I set up a local demo and went directly to the code.

@ RestControllerpublic class AsyncController {@ Autowired private AsyncService asyncService; @ GetMapping ("/ v1/say") public String sayV1 () {asyncService.sayV1 (); return "success1";} @ GetMapping ("/ v2/say") public String sayV2 () {asyncService.sayV2 (); return "success2" } @ Servicepublic class AsyncService {public void sayV1 () {try {Thread.sleep (3000L);} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("hello world");} @ Async public void sayV2 () {try {Thread.sleep (3000L) } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("hello world");}}

Very simple demo, provides two interfaces, / v1/say and / v2/say, one synchronous execution, one asynchronous execution, through sleep 3 seconds to simulate time-consuming tasks

Start normally without any problems. Wait 3 seconds for synchronous execution before the main thread will return, asynchronous execution will return immediately, and wait 3 seconds to output helloworld.

But when I added the breakpoint, the problem arose.

I first added a breakpoint to the line printing hello world, and the effect was the same as the original, but it was blocked before printing, but it did not affect the return of the main thread.

Editing

But when I added the breakpoint to the position where the method came in, I found that the main thread was blocked!

Editing

IV. Solution

For various problems, @ Async did not take effect, and the asynchronous task waited for the return of the main thread, but did not find an effective solution.

Later, I was reminded by a colleague, could it be a thread blocked by the debug function?

With the attitude of giving it a try, I found the configuration on the debug side

Editing

Breakpoints can choose to block jvm or block the current thread. The default is blocking jvm.

Select Thread for suspend, and the main thread will no longer be blocked.

At this point, I believe you have a deeper understanding of "java Asynchronous Task instance Analysis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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