The difference between concurrent parallel synchronous asynchronous multithreading

Source: Internet
Author: User

1. Concurrency: InOperating System processor run. Two of these concurrency relationships are synchronous and mutually exclusive
2. Mutex: The use of critical resources between processes is called mutual exclusion.
3. Sync: There are 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 a multiprocessor system, 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.

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.

6. Async: Asynchronous and synchronous are 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.


For a better understanding of the above concepts, let's say that I'm going to do boiling water, lifting the barbell 100, washing clothes 3 things.

Boil boiling water this thing, I want to do for, prepare to boil boiling water for 1 minutes, and so boiling water boil 8 minutes, turn off the boiling machine 1 minutes
Lifting the Barbell 100 under what I'm going to do for, lift the barbell 100 under 10 minutes
Wash the clothes I want to do for, prepare to wash clothes 1 minutes, wait for boiling water boil 5 minutes, turn off the washing machine 1 minutes

Single-Core case
To complete the synchronization, I need to do the time for 1+ 8 +1 + 10 + 1+ 5 +1 = 27 minutes

If it's asynchronous, it's waiting, I can switch to something else.

Prepare boiled water (1) + Ready to wash (1) + Lift 50 Barbell (5) min + Off washing machine 1 min + Lifting Barbell 20 under (2) min + Off boiling machine 1 min + Lifting 30 down Barbell (3) min
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

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.