Home >Common Problem >What is the difference between concurrency and parallelism?
Difference: Parallelism means that multiple instructions are executed simultaneously on multiple processors at the same time; both from a micro and macro perspective, both are executed together. Concurrency means that only one instruction can be executed at the same time, but multiple process instructions are executed in rapid rotation, which has the effect of multiple processes executing at the same time from a macro perspective, but not simultaneously from a micro perspective.
Concurrency and parallelism are:
Explanation 1: Parallelism means Two or more events occur at the same time; concurrency means that two or more events occur at the same time interval.
Explanation 2: Parallelism is multiple events on different entities, and concurrency is multiple events on the same entity.
Explanation 3: Parallelism is the simultaneous processing of multiple tasks on multiple processors. For example, in a Hadoop distributed cluster, concurrency means processing multiple tasks "simultaneously" on one processor.
So the goal of concurrent programming is to make full use of each core of the processor to achieve the highest processing performance.
Parallel: refers to multiple instructions being executed simultaneously on multiple processors at the same time. So whether from a micro or macro perspective, the two are executed together.
Concurrency: refers to that only one instruction can be executed at the same time, but multiple process instructions are executed in rapid rotation, making it macroscopically The effect of multiple processes executing at the same time, but in a microscopic sense, is not executed at the same time. It just divides the time into several segments so that multiple processes can execute quickly and alternately.
Parallelism exists in multi-processor systems, while concurrency can exist in both single-processor and multi-processor systems. Concurrency can exist in single-processor systems. Because concurrency is the illusion of parallelism, parallelism requires the program to perform multiple operations at the same time, while concurrency only requires the program to pretend to perform multiple operations at the same time (performing one operation per small time slice, and quickly switching execution of multiple operations).
When there are multiple threads operating, if the system has only one CPU, it is impossible to actually 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 in a time period is running, other threads are in a suspended state. This method is called concurrency (Concurrent).
When the system has more than one CPU, the thread operations may not be concurrent. When one CPU executes one thread, another CPU can execute another thread. The two threads do not seize each other's CPU resources and can proceed at the same time. This method is called parallel (Parallel).
For more related knowledge, please visit: PHP Chinese website!
The above is the detailed content of What is the difference between concurrency and parallelism?. For more information, please follow other related articles on the PHP Chinese website!