Home > Java > Javagetting Started > body text

What are the three major characteristics of multithreading?

王林
Release: 2020-04-27 16:32:05
forward
3158 people have browsed it

What are the three major characteristics of multithreading?

Multi-threading has three major characteristics, namely:

Atomicity, visibility, orderliness

1. What is an atom? Nature

That is, one operation or multiple operations are either fully executed and the execution process will not be interrupted by any factors, or they are not executed at all.

A very classic example is the bank account transfer problem:

For example, if you transfer 1,000 yuan from account A to account B, it must include two operations: subtract 1,000 yuan from account A, and then transfer 1,000 yuan to account B. Account B adds 1,000 yuan. These two operations must be atomic to ensure that no unexpected problems occur.

The same is true when we operate data, such as i = i 1; which includes reading the value of i, calculating i, and writing i. This line of code is not atomic in Java, so multi-threaded operation will definitely cause problems, so we also need to use synchronization and lock to ensure this feature.

(Video tutorial recommendation: java video)

2. What is visibility

When multiple threads access the same When a variable is modified and one thread modifies the value of the variable, other threads can immediately see the modified value.

If the two threads are on different CPUs, then thread 1 changes the value of i and has not refreshed it to the main memory, and thread 2 uses i again, then the value of i must still be the same as before. Thread 1 has nothing to do with the variable The modification thread 2 didn't see was a visibility issue.

3. What is orderliness?

The order of program execution is executed in the order of code.

Generally speaking, in order to improve the efficiency of program operation, the processor may optimize the input code. It does not guarantee that the execution order of each statement in the program is consistent with the order in the code, but it will ensure that the program is ultimately executed. The result is consistent with the result of sequential execution of the code. As follows:

int a = 10; //语句1
int r = 2; //语句2
a = a + 3; //语句3
r = a*a; //语句4
Copy after login

Due to reordering, he may also execute the order 2-1-3-4, 1-3-2-4, but it is absolutely impossible to 2-1-4-3, because this Dependencies are broken.

Obviously reordering will not cause any problems for single-threaded operation, but not necessarily for multi-threading, so we have to consider this issue when programming with multi-threads.

Recommended tutorial: Getting started with java

The above is the detailed content of What are the three major characteristics of multithreading?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template