In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 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 process threading, synchronous asynchronism, blocking non-blocking, and concurrent parallelism". The explanation in this article is simple and clear and easy to learn and understand. let's study and learn "what is process threading, synchronous asynchronism, blocking non-blocking, and parallel"!
Basic concept 1 processes and threads
Process (Process):
It is a basic concept in Windows system, which contains the resources needed to run a program. A running application is treated as a process in the operating system, and the process can include one or more threads. Threads are the basic unit in which the operating system allocates processor time, and multiple threads can execute code at the same time in a process. Processes are relatively independent, one process cannot access the data of another process (unless using distributed computing), and the failure of one process will not affect the operation of other processes. Windows system uses processes to divide the work into multiple independent areas. A process can be understood as the basic boundary of a program. Is a running routine of the application, is a dynamic execution process of the application.
Thread (Thread):
It is the basic execution unit of a process and the basic unit of CPU time allocated by the operating system. A process can contain several threads, and the first thread executed at the process entrance is regarded as the main thread of the process. In .NET applications, the Main () method is used as the entry, and when this method is called, the system automatically creates a main thread. Threads are mainly composed of CPU registers, call stacks and thread local memory (Thread Local Storage,TLS). The CPU register mainly records the status of the current executing thread, the call stack is mainly used to maintain the memory and data called by the thread, and the TLS is mainly used to store the state information of the thread.
The difference between processes and threads
The main difference between processes and threads is that they are different ways of managing operating system resources. A process has an independent address space. After a process crashes, it will not affect other processes in protected mode, and threads are just different execution paths in a process. Threads have their own stacks and local variables, but there is no separate address space between threads, so the death of a thread equals the death of the whole process, so a multi-process program is more robust than a multi-thread program, but when a process is switched, it consumes more resources and is less efficient. However, for some concurrent operations that require simultaneous and shared variables, only threads can be used, not processes.
1) in short, a program has at least one process and a process has at least one thread.
2) the division scale of threads is smaller than that of processes, which makes multithreaded programs have high concurrency.
3) in addition, the process has an independent memory unit during execution, and multiple threads share memory, which greatly improves the running efficiency of the program.
4) there is a difference between a thread and a process during execution. Each independent thread has an entrance to the program, a sequential execution sequence, and an exit to the program. However, threads cannot execute independently and must be stored in the application, which provides multiple thread execution control.
5) from a logical point of view, the significance of multithreading is that there are multiple execution parts in an application that can be executed at the same time. However, the operating system does not regard multiple threads as multiple independent applications to achieve process scheduling and management and resource allocation. This is the important difference between processes and threads.
2 synchronous (Sync) and asynchronous (Async)
Synchronization:
Synchronization means that when a function call is made, the call does not return or continue to perform subsequent operations until the result is obtained.
To put it simply, synchronization means that you must do one thing at a time, and wait until the previous one is done before you can do the next thing.
For example: form submission in BPUBG S mode, the specific process is: the client submits the request-> waits for the server to process-> returns after processing, and the client (browser) cannot do anything else during this process.
Async:
Async is the opposite of synchronization, and when an asynchronous procedure call is made, the caller can continue to perform subsequent operations before getting the result. When this call is complete, the caller is generally notified through status, notification, and callback. For asynchronous calls, the return of the call is not controlled by the caller.
There are three ways to notify the caller, as follows:
Status
That is, monitoring the status of the callee (polling), and the caller needs to check it at regular intervals, which will be very inefficient.
Notice
When the callee's execution is complete, a notification is issued to inform the caller that there is no need to consume much performance.
Callback
Similar to notification, when the callee's execution is complete, the callback function provided by the caller is called.
For example: the ajax request in BPUBERS mode, the specific process is: the client sends out the ajax request-> the server handles-> executes the client callback after processing, and after the client (browser) makes the request, you can still do other things.
The difference between synchronous and asynchronous:
To sum up, the difference between synchronous and asynchronous: whether you need to wait for the result after the request is made before you can continue to perform other operations.
3 blocking and non-blocking
The concepts of blocking and non-blocking are related to the state of the program (thread) waiting for message notification (no matter synchronous or asynchronous). That is to say, blocking and non-blocking are mainly from the point of view of the state of the program (thread) waiting for message notification.
Blocking and non-blocking are concerned with the state of the program while waiting for the result of the call (message, return value).
A blocking call means that the current thread is suspended before the result of the call is returned. The calling thread does not return until it gets the result.
A non-blocking call means that the call does not block the current thread until the result is not immediately available.
4 concurrent parallelism
Concurrency: in an operating system, it means that several programs in a period of time are between starting and running, and these programs are all running on the same processor, but there is only one program running on the processor at any one time. When there are multiple threads in operation, if the system has only one CPU, it is impossible for it to really run more than one thread at the same time. It can only divide the CPU running time into several time periods, and then allocate the time period to each thread for execution. When the thread code of a time period is running, other threads are suspended. . This approach is called Concurrent.
Parallelism: when the system has more than one CPU, thread operations are likely to be non-concurrent. When one CPU executes one thread, another CPU can execute another thread. The two threads do not preempt CPU resources and can do so at the same time, which is called Parallel.
The difference between concurrency and parallelism:
When you are in the middle of eating, the phone comes, and you don't answer it until you finish eating, which means you don't support concurrency or parallelism.
When you are in the middle of eating, the phone comes, you stop to answer the phone, and then continue to eat, which shows that you support concurrency.
When you are in the middle of eating, the phone comes, and you eat while you are on the phone, which shows that you support parallelism.
The key to concurrency is your ability to handle multiple tasks, not necessarily at the same time. The key to parallelism is your ability to handle multiple tasks at the same time.
So I think the most important point is whether they are "at the same time" or not.
The difference between async and multithreading? (principles) 1 what's the difference between async and multithreading?
In fact, asynchronism is the goal, and multithreading is the way to achieve this. Async means that after An initiates an operation (usually a time-consuming operation, there is no need for asynchrony if it is not time-consuming), it can continue to deal with its own business without waiting for the time-consuming operation to return.
2 similarities and differences between multithreaded and asynchronous operations
Both multithreading and asynchronous operations can avoid calling thread blocking, thus improving the responsiveness of the software. Sometimes we even think that multithreading and asynchronous operations are the same concept. However, there are some differences between multithreaded and asynchronous operations. These differences lead to the difference in timing between multithreaded and asynchronous operations.
3 the nature of asynchronous operation
All programs will eventually be executed by computer hardware, so in order to better understand the nature of asynchronous operation, it is necessary to understand its hardware basis. Friends who are familiar with computer hardware must be familiar with the word DMA. There are clear DMA model indicators in the technical specifications of hard drives and optical drivers. in fact, network cards, sound cards and graphics cards also have DMA functions. DMA means direct memory access, that is, hardware with DMA function can exchange data with memory without consuming CPU resources. As long as the CPU sends an instruction when initiating the data transfer, the hardware starts to exchange data with the memory, and after the transfer is completed, the hardware will trigger an interrupt to notify the completion of the operation. These CPU O operations, which do not need to consume any time, are the hardware foundation of asynchronous operations. So asynchronous DMA operations can be initiated even in a single-process (and wireless-range concept) system like DOS.
4 the nature of threads
Thread is not a function of computer hardware, but a logic function provided by the operating system. A thread is essentially a piece of code running concurrently in a process, so a thread needs the operating system to invest CPU resources to run and schedule.
5 advantages and disadvantages of asynchronous operation
Because asynchronous operations do not require additional thread burden and are handled by callbacks, handlers do not have to use shared variables in well-designed cases (even if they cannot be used at all, at least the number of shared variables can be reduced), reducing the possibility of deadlock. Of course, asynchronous operation is not perfect. The complexity of writing asynchronous operation is high, and the program is mainly processed by callback, which is new to the way of thinking of ordinary people, and difficult to debug.
6 advantages and disadvantages of multithreading
The advantage of multithreading is obvious, the processing program in the thread is still executed sequentially, which is in line with the thinking habits of ordinary people, so the programming is simple. But the disadvantage of multithreading is also obvious, the use of threads (abuse) will bring the additional burden of context switching to the system. And shared variables between threads may cause deadlocks.
Asynchronism and multithreading, from a dialectical point of view, asynchronism and multithreading are not equal from time to time. Asynchronism is the end, and multithreading is just a means for us to realize asynchronism. What is async: async is when a call request is sent to the callee and the caller does not have to wait for its result to return. Asynchrony can be implemented using multithreading technology or left to another process to handle
What is the difference between asynchronism, multithreading and parallelism? (story)
For non-professionals, let's explain it in non-professional language. The metaphor is not appropriate enough, but it probably means that.
Let me tell you a story first:
That was 10 years ago, and before 12306, people had to go to the railway station to buy tickets. Because everyone wants to go home for the Spring Festival, do not want to wait, there is only one railway station, there are only so many windows, headache ah. What is even more troublesome is that the person who comes to the window is picky, not expensive, not at night, and do not stand. With all kinds of ink stains with the conductor, the people behind were even more anxious, filled with righteous indignation and scolded their father and mother.
Now suppose there is only one train and one conductor in the whole city. Each passenger needs to think for a minute before deciding which train to buy after consulting the conductor.
1. Async: after consulting the ticket buyer, you need to think for 1 minute and stand aside immediately, but you don't have to wait in line again. When you can figure out when to buy a ticket with the conductor immediately. While the person was standing by thinking, the person behind hurriedly went up and bought it. At this time, the team is moving very quickly, there is no congestion, and the conductor maximizes the efficiency.
two。 Multithreading: the train station has n windows (but only one person sells tickets), and there are n queues outside at the same time. After answering the consultant's questions, the conductor immediately goes to the next window and then rotates to the next window. When the person at which window decided, the conductor immediately went over to buy it for him. At this time, the passengers are relatively simple, but if someone in that team thinks about it for a long time, the people behind will be tragic.
3. Parallel: copy n railway stations and sell tickets at the same time, the ability to buy tickets is greatly enhanced. You can also buy tickets at any railway station where there are few people.
It can be seen: in the case of only one railway station and only one conductor, selling one after another will lead to a waste of resources, inefficiency, the team is stuck, and it is difficult to move forward. 1. The optimization methods of 1 and 2 have solved the problems of motionless team and low ticket sales rate. But it is a good way to increase the number of railway stations, windows and conductors.
Conclusion:
1. In fact, the efficiency of asynchronous and multithreading is about the same, but there are not many windows, such as 3, and when many people spend 5 minutes instead of 1 minute to struggle, the efficiency of multithreading is actually lower than that of asynchronousness. because the conductor still often encounters three teams stuck at the same time when they can't buy tickets.
two。 It is also a bit inappropriate to compare these two concepts, because they are not a concept, the purpose of multithreading is to achieve asynchronism, and multithreading should be a means to achieve asynchrony. Asynchrony should be compared with synchronization.
3. Multithreading is relatively simple, but it requires additional windows to increase the cost, and the conductor is tired. This is similar to the relationship between php under apache and javascript under node.js. One is multithreaded, but blocking, and the other is single-threaded asynchronous non-blocking. Php's solution is more in line with conventional thinking, but the comparison costs memory, node.js is non-blocking, and the same task can be accomplished with less resources, but programming is more laborious.
4. Parallel, similar to using each core of multicore cpu to calculate at the same time. Concurrency can be divided into pseudo concurrency and true concurrency. The former refers to the concurrency of single-core processors, while the latter refers to the concurrency of multi-core processors.
5. The ultimate solution is parallel computing, and asynchronous computing under each cpu, so that each of your cores can be fully utilized. It's just that the programming requirements are too high, if it is not intensive computing, such as large-scale finite element computing (mostly concurrent), or the server handles thousands of visits at the same time (mostly asynchronous or multithreaded). Let's honestly use the traditional method, after all, the amount of computation of conventional programs is not a big problem for today's hardware.
Fourth, what is the difference between blocking, non-blocking and synchronous async? (story)
Understand synchronous blocking, synchronous non-blocking, asynchronous non-blocking
Synchronous / asynchronous is concerned with the mechanism of message notification, while blocking / non-blocking is concerned with the state of the program (thread) while waiting for message notification.
Take Xiaoming download file as an example to explain these two sets of concepts again from these two concerns, hoping to better promote everyone's understanding.
Synchronization blocking: Xiao Ming keeps an eye on the download progress bar and finishes it by 100%.
Synchronization is reflected in: waiting for the download completion notice
The blocking is reflected in that you cannot do other tasks while waiting for the download completion notification.
Synchronous non-blocking: Xiaoming does something else after submitting the download task, glancing at the progress bar every once in a while and completing it when he sees 100%.
Synchronization is reflected in: waiting for the download completion notification, but in the
Non-blocking is reflected in: waiting for the download completion notice to do other tasks, but occasionally glancing at the progress bar; [Xiao Ming must switch between the two tasks and pay attention to the download progress]
Asynchronous blocking: Xiaoming changed a software with download completion notification function, and the download was completed with a "Ding" sound. But Xiao Ming has been waiting for the sound of "Ding" (it looks silly, doesn't it).
Asynchronism is reflected in the notification of "Ding" when the download is completed.
The blocking is reflected in that you cannot do other tasks while waiting for the download to complete the "Ding" notification.
Asynchronous non-blocking: it is still the download software that will "Ding". After Xiaoming submits the download task, he goes to do something else and knows it is done when he hears the "Ding" sound.
Asynchronism is reflected in the notification of "Ding" when the download is completed.
Non-blocking is reflected in: waiting for the download to complete the "Ding" notification process, to do other tasks, just need to receive the "Ding" notification; [the software handles the download task, Xiaoming handles other tasks, and there is no need to pay attention to the progress. Just receive the software "Ding" notification, you can]
In other words, synchronous / asynchronous is the way (mechanism) of "download completion message" notification, while blocking / non-blocking is the state while waiting for "download completion message" notification (whether it can do other tasks). In different scenarios, the four combinations of synchronous / asynchronous and blocking / non-blocking are applied.
So, to sum up, synchronization and asynchronism are only mechanisms concerned with how messages are notified, while blocking and non-blocking are concerned with the state while waiting for message notifications. In other words, in the case of synchronization, it is up to the messenger to wait for whether the message is triggered, while in the case of asynchronism, the trigger mechanism notifies the messenger, so in the asynchronous mechanism, a bridge is needed between the message processor and the trigger mechanism:
In Xiao Ming's case, the bridge is the sound of software "Ding".
Synchronous / asynchronous and blocking / non-blocking #
1 synchronous blocking form
Efficiency is the lowest.
Take the above example, where you concentrate on waiting for the download to complete and do nothing else.
In the actual program: it is the read/write operation that does not set the O_NONBLOCK flag bit on the fd.
2 form of asynchronous blocking
An asynchronous operation can be blocked, except that it is not blocked while processing the message, but while waiting for the message to be notified.
For example, the select function, if the last timeout parameter passed in is NULL, then if none of the concerned events are triggered, the program will block the select call all the time.
3 synchronous non-blocking form
It's actually inefficient.
Imagine that while you are doing something else, you need to look up to see if the download is complete. If you think of doing something else and observing the location of the download as two operations of the program, the program needs to switch back and forth between these two different behaviors, which is conceivably inefficient.
Many people will write blocking read/write operations, but don't forget to set the O_NONBLOCK flag bit on fd so that synchronization operations can be made non-blocking.
4 Asynchronous non-blocking form
More efficient
Because it's your job to wait for the download to complete, and it's the computer's job to notify you, the program doesn't switch back and forth between two different operations.
So far, we have talked about process threads, synchronous asynchronism, blocking non-blocking, and concurrent parallelism. Please point out some bad things. At the same time, I would also like to thank the online boss's article for helping me understand these concepts.
Thank you for your reading, the above is "what is process thread, synchronous asynchronous, blocking non-blocking, concurrent parallel" content, after the study of this article, I believe you have a deeper understanding of what is process thread, synchronous asynchronous, blocking non-blocking, concurrent parallelism, and the specific use needs to be verified in practice. 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.