Concurrency theory

1. Concurrency: a state. Multiple programs can only execute one at the same time, and it takes a period of time to execute all programs successively.
   Parallel: A state. Multiple programs can be executed simultaneously at the same time.
   Competition: a state. Before the shared resource is obtained, multiple programs simultaneously or sequentially obtain the behavioral state of the resource.

2. Concurrency characteristics: there are shared resources; all programs are executed successively (the shared resources may be obtained successively, such as single-core cpu).
   Parallel characteristics: no shared resources; execute all programs at the same time (such as multi-core cpu).
   Competitive features: Neither have shared resources yet; not necessarily simultaneous.

3. Concurrency may or may not lead to competition (such as two men chasing a beautiful woman, and one of them gives up the competition).
   Concurrency can lead to thread unsafety.

4. Concurrency conditions: 1. There are shared resources; 2. Multiple programs acquire shared resources at the same time.

5. Data race:
   Race conditions:

6. Factors that cause thread unsafety: state.

7. Status: that is, data. This data is stored in state variables.
   State characteristics: Internally variable, externally shared.

8. What is thread safety: under a single thread, what you see is what you get. Under multi-threading, continuous and correct execution. Multithreading is less dangerous than single threading.

9. How to ensure thread safety:
   stateless;
   when there is one state (using final constants; no sharing; volatile; atomic objects);
   when there are multiple states (avoiding state dependencies; synchronization).

10. Atomicity: A separate, indivisible operation or process.

11. Visibility: What thread A does in the synchronization block area (referring to the state modification of the shared object) can also be seen by thread B after acquiring the lock.

12. Reordering: For at least two operations, the order of operations is originally from A to B, but in a multi-threaded environment, it may be changed to execute B first, and then execute A.

13. Reentrancy: After thread A enters the synchronization block of the lock object, it can continue to enter different synchronization blocks of the same lock object without exiting the block.

14, lock: is an object. A synchronized block can be locked. Mutual exclusion, atomicity, visibility, reentrancy, avoid reordering.

15. Synchronization: lock or use volatile.

16, volatile: is a weak synchronization. The original meaning is volatile and changeable.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326747763&siteId=291194637