Concurrency and parallelism, asynchronous and multithreading differences

Source: Internet
Author: User

1. Concurrency and parallelism differences
the difference between the two can be identified by the image:1) Definition: Concurrency: in the operating system, there are several programs in a period of time that are running from the start to completion, and these programs are running on the same processor , but at any point there is only one program Run on the processing machine. parallelism: in the operating system, a set of programs is executed independently and asynchronously , and the programs are executed together , both from the micro and macro level. to a metaphor: concurrency and parallelism is the difference between a person eat three steamed bread and three people at the same time eat three steamed bread;

In a single CPU system , the system is scheduled to run only one thread at a time, although there are many forms of this debug mechanism (mostly time-slice rounds), but in any case, it is called concurrency by constantly switching the threads that need to be run (concurrent ). In a multi-CPU system , you can allow more than two threads to run concurrently, which can be called parallel (parallel)in the same way that more than two threads run simultaneously.

2) concurrency usually refers to improving the performance of programs running on a single processor ;

Concurrency is stateful , "has demonstrable certainty, but is actually nondeterministic";

"Concurrency" is not at the same time in the micro-execution, but the time is divided into several paragraphs, so that multiple processes quickly alternating execution, from the macroscopic perspective, it seems that these processes are executing.

Using multiple threads can help us achieve higher throughput in a single processing system, and if a program is single-threaded, the processor is still idle while waiting for a synchronous I/O operation to complete. In a multithreaded system, when a thread waits for I/O, other threads can execute.



second, asynchronous and multi-threaded1) Basic Concepts

1. Concurrency: InOperating System, there are several programs in a time period that have been started and run, and the programs are all in the sameProcessorrun on. Two of these concurrency relationships are synchronous and mutex, respectively
2. Mutual exclusion: The phenomenon of the use of critical resources between processes is called mutex.
3. Sync:The relationship between processes is not the relationship of mutually exclusive critical resources, but the interdependent relationship. Further note: The output of the previous process is the input of the latter process, and the second process must wait when the first process does not output. A group of concurrent processes that have synchronization relationships send each other information called messages or events.
There is concurrent pseudo concurrency and true concurrency, pseudo-concurrency refers to the single-core processor concurrency, true concurrency refers to the multi-core processor concurrency.
4. Parallel: In a single processor in a multi-channel program design system, the process is alternately executed, showing a concurrency of external special; In multiprocessor systems, processes can be executed alternately and overlapped. A program on a multiprocessor can implement parallel processing. In this sense, parallelism is for multi-processor. Parallel is a concurrent occurrence of multiple simultaneous events, with the meaning of concurrency, but concurrency is not necessarily parallel, it is also said that concurrent events do not necessarily occur at the same time.

5. Multithreading: Multithreading is the logical layer concept of programming, which is a piece of code that runs concurrently in a process. Multithreading enables switching between threads to execute.

< Span style= "color: #333333; Background-color:inherit ">    6. Async: Asynchronous and synchronous are relative, synchronization is sequential execution, execution of one after the next, need to wait, Coordinated operation. Asynchronous is independent of each other, in the process of waiting for an event to continue to do their own things, do not have to wait for the event to complete and then work. A thread is a way to implement Asynchrony. Async is the main thread that lets the calling method do not need to wait for another thread to finish synchronously, so that the main thread can Cheng Gan other things.

< Span style= "font-size:12px" >
     Async and multithreading are not an equal relationship Async is the ultimate goal, Multithreading is just a means for us to implement Asynchrony

The difference between asynchronous and synchronous , when IO waits, synchronization will not cut away, wasting time.

Multi-Threading Benefits , it is easier to implement the idea of asynchronous switching, because the asynchronous program is difficult to write. Multithreading itself is still done synchronously, but should be said to be less efficient than asynchronous. and the multi-line is very easy to write, relative efficiency is also high.

2) Deep understanding

The similarities and differences between multithreading and asynchronous operations

both multithreaded and asynchronous operations can be achieved avoid calling thread blocking To improve the responsiveness of the software. Even sometimes we think that multithreading and asynchronous operations are the same concept. However, there are some differences between multithreading and asynchronous operations. These differences create the difference between the timing of using multithreading and asynchronous operations.

The nature of asynchronous operations

All the programs will eventually be executed by the computer hardware, so in order to better understand the nature of the asynchronous operation, we need to understand its hardware base. Familiar with computer hardware friend is certainly not unfamiliar with the word DMA, hard disk, optical drive technical specifications have a clear DMA Span style= "color: #232323; Background-color:inherit "" The mode indicator, in fact, the network card, sound card, video card also has DMA function. DMA is direct memory access these No need to consume CPU time i/ O operation is the hardware foundation of the asynchronous Operation

The nature of Threads
A thread is not a function of a computer's hardware, but rather a logical function provided by the operating system , which is essentially a piece of code that runs concurrently in a process, so the thread requires the operating system to run and dispatch the CPU resources.

Advantages and disadvantages of asynchronous operations

< Span style= "font-size:12px" > Because asynchronous operations do not have to be burdened by additional threads and use callback method processing, in a well-designed case, the handler function can You do not have to use the shared variable reduced the possibility of a deadlock

Advantages and disadvantages of multithreading
The advantages of multithreading are obvious, the handlers in the thread are still sequential execution, in line with the normal thinking habits, so programming is simple. However, the disadvantage of multithreading is equally obvious, the use of threads (misuse) will bring the burden of context switching to the system . Also, shared variables between threads can cause deadlocks to occur.

Scope of application

< Span style= "font-size:12px" >
and the thread applies to that kind of





























Concurrency and parallelism, asynchronous and multithreading differences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.