The difference between concurrent parallel synchronous asynchronous multithreading blocking non-blocking

Source: Internet
Author: User

1. Concurrency (concurrency): In the operating system , refers to a period of time a few programs are in the start run until the run is complete, And these programs are all running on the same processor . Two of these concurrency relationships are synchronous and mutex, respectively

    • Mutex: The use of critical resources between processes is called mutual exclusion.
    • Sync (synchronous): 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.
2. Parallel (parallelism): 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 can overlap. 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.

3. 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.

4. Asynchronous (asynchronous): Asynchronous and synchronous is relative, synchronization is sequential execution, execution of one after execution of 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.
Async and multithreading are not an equal relationship, asynchronous is the ultimate goal, multithreading is just a means for us to implement Asynchrony . Asynchronous is when a call request is sent to the callee, and the caller can do something else without waiting for the return of the result. The implementation of Asynchrony can be handled by multithreading or by handing it to another process.

5. blocking: A blocking call means that the current thread is suspended until the call results are returned. Functions are returned only after the result is obtained.

6. non-blocking: the concept of nonblocking and blocking corresponds to a function that does not block the current thread and returns immediately until the result is not immediately available.


Synchronization is corresponding to Async, which is a relationship between threads, and two threads are either synchronous or asynchronous. Blocking and non-blocking are for the same thread, and at some point the thread is either blocked or non-blocking. Blocking is the result of using the synchronization mechanism, and non-blocking is the result of using the asynchronous mechanism.

=========================== ====================================================================
for a better understanding of the above concepts give a simple example,   suppose I'm going to do Boil boiling water, lift the Barbell 100, wash the clothes 3 things.

Boiling water This thing,   I want to do for, prepare to boil boiling water for 1 minutes, wait for boiling water boil for 8 minutes, turn off the boiling machine 1 minutes
lift the Barbell 100 under          I Things to do for,   lifting Barbell 100   10 minutes
Laundry              What I'm going to do for, ready to do the laundry 1 minutes, wait for boiling water for 5 minutes, turn off the washing machine for 1 minutes

Single-core case
synchronization completed, I need to do time for 1+ 8 +1 + 10 + 1+ 5 +1 = 27 min

If asynchronous, just wait, I can switch to do something else
Prepare boiling water (1) + Prepare to wash clothes (1) + Lift 50 Barbell (5) minutes + off washing machine 1 minutes + Lift Barbell 20 under (2) minutes + off boiling machine 1 min + Lifting 30 down Barbell (3) minutes
1+1+5+1+2+1+3 = 14 minutes


Dual-core asynchronous parallel

Nuclear 1 prepare to boil boiling water for 1 minutes + Lift Barbell 50 under (5) minutes + wait 3 minutes + turn off the boiler for 1 minutes

Nuclear 2 ready to wash clothes 1 minutes + lift Barbell 50 under (5) minutes + turn off the washing machine 1 minutes + wait 3 minutes

In fact, it only took 1+5+3+1 = 10 minutes

There are two cores waiting for 3 minutes.

Dual-core asynchronous non-parallel

Nuclear 1 Lifting Barbell 100 (10) min

Nuclear 2 prepare to boil boiling water 1 minutes + prepare to wash clothes 1 minutes + wait 5 minutes + + Turn off the boiler 1 minutes + wait 1 minutes + turn off the washing machine for 1 minutes

In fact, it only took 1+5+3+1 = 10 minutes

Multi-threaded approach
Single Core

Thread 1 prepare to boil boiling water for 1 minutes, wait for boiling water for 8 minutes, turn off the kettle for 1 minutes
Thread 2 Lifting Barbell 100 under 10 minutes
Thread 3 Prepare to wash for 1 minutes, wait for boiling water to boil for 5 minutes, turn off the washing machine for 1 minutes

The CPU may switch the most ideal switching mode

Thread 1 ready to boil boiling water 1 sleep 1 sleep 5 sleep 1 sleep 2 off boiling water 1 minutes exit
Thread 2 Sleep 1 sleep 1 Lifting Barbell 50 5 minutes sleep 1 Lifting Barbell 20 2 min sleep1 Lifting Barbell 30 next 3 minutes
Thread 3 Sleep 1 ready to wash clothes 1 minutes sleep 5 off washing machine 1 minutes exit

The last use of 14 minutes is the same as async.
But actually it is not the same, because the thread will not run according to our assumptions, if the thread 2 barbell first run, the speed of the entire process down.

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

If all are exclusive CPU business, such as lifting the barbell business, in the case of single-core multi-line and single-wire no difference.

Multithreading 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 it should be said
than efficiency is not as asynchronous. and the multi-line is very easy to write, relative efficiency is also high.

The benefit of multicore is that you can do things at the same time, which is completely different from the single core.

The difference between concurrent parallel synchronous asynchronous multithreading blocking non-blocking

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.