Differences between concurrent synchronization and asynchronous multithreading: asynchronous Multithreading

Source: Internet
Author: User

Differences between concurrent synchronization and asynchronous multithreading: asynchronous Multithreading

1. Concurrency: in the operating system, several programs in a period of time are between started and running, and these programs are all running on the same processing machine. The two types of concurrency are synchronization and mutex.
2. Mutual Exclusion: the use of critical resources between processes is called mutual exclusion.
3. Synchronization: the relationship between processes is not the relationship between mutually exclusive critical resources, but the relationship between them. Further description: the output of the previous process is used as the input of the next process. When the first process has no output, the second process must wait. Messages or events are sent by a group of concurrent processes with synchronization relationships.
Concurrency includes both pseudo concurrency and real concurrency. pseudo concurrency refers to the concurrency of a Single-core processor, and real concurrency refers to the concurrency of a multi-core processor.
4. parallel: in a single-processor multi-channel program design system, processes are executed alternately, showing a concurrent external special feature. In a multi-processor system, processes can not only be executed alternately, in addition, it can overlap execution. A program on a multi-processor can implement parallel processing. We can see that parallelism is for multi-processor. Parallel is a concurrent event that occurs at the same time and has the meaning of concurrency, but concurrency is not necessarily parallel. It also means that concurrent events do not necessarily occur at the same time.

5. multithreading: multithreading is the logic layer concept of programming. It is a piece of code that runs concurrently in a process. Multiple Threads can switch between threads.

6. asynchronous: asynchronous and synchronous are opposite. synchronous means sequential execution. After one execution is complete and then the next execution is executed, it is necessary to wait and coordinate the operation. Asynchronization is independent of each other. You do not need to wait for the event to complete before proceeding. Threads are asynchronous. Asynchronous means that the main thread that calls the method does not need to wait for the completion of another thread synchronously, so that the main thread can do other things.
Asynchronization is not the same as multithreading. Asynchronization is the ultimate goal. multithreading is just a means for us to implement Asynchronization. Asynchronous means that when a call request is sent to the called user, the caller can do other things without waiting for the return of the result. To implement Asynchronization, you can use multithreading technology or hand it over to another process for processing.


For a better understanding of the above concepts, let's take a simple example. Suppose I want to cook boiling water, take the barbell 100 and wash three things.

I want to boil the water for 1 minute. I want to boil the water for 8 minutes and turn off the water burning machine for 1 minute.
What I want to do at barbell 100 is to raise barbell 100 for 10 minutes.
What I want to do is prepare to wash clothes for 1 minute, wait for boiling water to boil for 5 minutes, and turn off the washing machine for 1 minute.

Single-core
Synchronization is completed. The time required is 1 + 8 + 1 + 10 + 1 + 5 + 1 = 27 minutes.

If it is asynchronous, I can switch to another task while waiting.

Prepare boiling water (1) + prepare washing clothes (1) + raise 50 barbell (5) minutes + close the washing machine for 1 minute + lift the barbell for 20 times (2) minutes + 1 minute + 30 minutes
1 + 1 + 5 + 1 + 2 + 1 + 3 = 14 minutes


Dual-core Asynchronous Parallel

1. Prepare boiling water for 1 minute + 50 minutes for lifting barbell (5) + 3 minutes for waiting + 1 minute for turning off the water burning machine

Core 2 prepare to wash clothes for 1 minute + lift barbell for 50 minutes (5) + turn off the washing machine for 1 minute + wait for 3 minutes

Actually it only took 1 + 5 + 3 + 1 = 10 minutes

There are also dual-core instances that have been waiting for 3 minutes.

Dual-core asynchronous non-parallel

Nuclear 1 lifting barbell 100 (10) minutes

1 minute + 1 minute + 5 minutes + 1 minute + 1 minute + 1 minute + 1 minute + 1 minute + 1 minute + 1 minute + 1 minute

Actually it only took 1 + 5 + 3 + 1 = 10 minutes

Multithreading
Single-core and Low-core

Thread 1: Prepare boiling water for 1 minute, wait for boiling water for 8 minutes, and turn off the boiling water machine for 1 minute
Thread 2: Hold the barbell for 100 for 10 minutes
Thread 3 prepare to wash clothes for 1 minute, wait for boiling water to boil for 5 minutes, and turn off the washing machine for 1 minute

The best way to switch the cpu

Thread 1 prepare boiling water 1 sleep 1 sleep 5 sleep 1 sleep 2 close boiling water 1 minute exit
Thread 2 sleep 1 sleep 1 lift barbell 50 5 minutes sleep 1 lift barbell 20 2 minutes sleep1 lift barbell 30 next 3 minutes
Thread 3 sleep 1 prepare to wash clothes 1 minute sleep 5 close washer 1 minute exit

The last 14 minutes is the same as that of Asynchronization.
But it is actually different, because the thread will not run as we imagined. If thread 2 runs first, the speed of the whole process will get down.

The difference between Asynchronization and synchronization is that when I/O is waiting, synchronization will not be switched away, wasting time.

If the services are exclusively occupied by the cpu, for example, the business with a barbell, there is no difference between multi-line and single-line services in Single-core scenarios.

The advantages of multithreading make it easy to implement the idea of asynchronous switching, because asynchronous programs are hard to write. The multi-thread process is still synchronized, but it should be said that
The efficiency is not comparable to that of Asynchronization. In addition, it is easy to write multiple lines, and the relative efficiency is also high.

The advantage of multi-core is that you can do things at the same time, which is totally different from that of single-core.

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.