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

What is the difference between async and multithreading in Spring Boot

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the difference between asynchronism and multithreading in Spring Boot". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the difference between async and multithreading in Spring Boot".

Recently, in the study of asynchronous processing in Spring Boot, I found that there are many knowledge points related to async and multithreading, so first write a few articles about async and multithreading, and review or learn relevant knowledge points together. Let's start with the text:

Preface

In this article, we explain asynchronous programming and multithreaded programming in some easy-to-understand ways, and then introduce the differences between them.

What is asynchronous programming?

First, let's take a look at the asynchronous model. In the asynchronous model, multiple events are allowed to occur (process) at the same time. When a program calls a long function (method), it does not block the execution process of the program, and the program continues to execute. When the function is finished, the program can get the message that the execution is complete or can access the result of the execution (if there is a return value or needs to return a value).

Let's look at the difference between synchronous and asynchronous through an example. In the example, the program gets two files through the network and merges the two files:

In the above example, the solution in an asynchronous system is to open an additional thread for processing. The first thread gets the first file, the second thread gets the second file, and the second thread does not have to wait for the first thread to finish execution. When both threads get the corresponding results, resynchronize the operation of merging the results.

Let's take a look at another scene. The single-threaded method reads files in OS (operating system) and requires mathematical operations. In the asynchronous system, the program initiates the request to read the file in OS. Because the read operation is time-consuming, when waiting for the file to be read, the program will return the controller to CPU for mathematical operation.

In asynchronous programming, a function is usually provided for a more time-consuming function, and the parameter of the function contains an additional parameter for callback. This function is often called a callback function. When the time-consuming function is finished, the result is returned through the callback function. For more information about callback functions, please refer to the article "two classic examples to give you a thorough understanding of the java callback mechanism".

What is multithreaded programming

Multithreading refers to the simultaneous or parallel execution of multiple instructions (threads).

On single-core processors, multithreading often gives the illusion that programs are executing in parallel. In fact, the processor switches and schedules between multiple threads through a scheduling algorithm. Or switch threads according to the combination of external input (interrupt) and the priority of the thread.

On multi-core processors, threads are really running in parallel. Multiple processors execute multiple threads at the same time to achieve more efficient processing.

A simple example is to open two browser windows and download two files at the same time. Each window uses a new thread to download files, and there is no need between them to wait for whom to finish, but to download in parallel.

The following figure shows the flow of concurrent execution of a multithreaded application:

The difference between async and multithreading

From the above introduction, we can see that multithreading is all about the concurrent execution of functions. Asynchronous programming is about non-blocking execution between functions, and we can apply async to single-thread or multi-thread.

Therefore, multithreading is just an implementation of asynchronous programming.

For example, you and your friend decide to make lunch together. "Asynchronous" is when you say to your friend, "you go to the store to buy spaghetti, let me know when you come back, and then make lunch together." while you buy spaghetti, I'll prepare ketchup and drinks. "

And the "thread" is: "you boil the water, I'll heat the ketchup. When the water boils, tell me I'll put Italy in. When the ketchup is hot, you can add cheese. When both are done, you can sit down for dinner." In the thread example, we can see the event order of "When,Do", which represents the order of the instruction set set for each person (thread).

As you can see from the above example, multithreading is related to the specific executor, while asynchrony is related to the task.

Multithreading is the logical layer concept of programming, it is a piece of code running concurrently in the process, and it can realize the switching execution between threads.

Asynchronism and synchronization are relative. Async means to be independent of each other and continue to do your own thing while waiting for an event. You don't have to wait for the event to finish before you work.

Multithreading is one way to achieve asynchrony. Asynchronism is so that the main thread that calls the method does not have to wait for another thread to finish synchronously, thus allowing the main thread to do something else.

So in essence, asynchronism and multithreading are not the same relationship, asynchrony is the ultimate goal, multithreading is just a means to achieve asynchronism.

How to choose

In the face of multithreading and asynchronism, how should we choose? In fact, usually the basis for selection is that it mainly depends on performance.

So which model performs better for all the combinations between synchronous / asynchronous and single / multithreaded?

In short, for large-scale applications with a large number of Istroke O operations and different computations, the use of asynchronous multithreading helps to make full use of computing resources and can take care of non-blocking functions. This is also the threading model used by all operating systems.

The complexity of writing asynchronous operation is high, and the program is mainly processed by callback, which is different from the normal way of thinking, and it is difficult to debug. The use of multithreading (abuse) brings an additional burden of context switching to the system, and shared variables between threads may cause deadlocks.

Therefore, in the implementation of these two modes, it is often necessary to deal with resource competition, deadlock, shared resources and callback events.

Thank you for your reading, the above is the content of "what is the difference between asynchronism and multithreading in Spring Boot". After the study of this article, I believe you have a deeper understanding of what is the difference between async and multithreading in Spring Boot. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report