Deep understanding of Java Virtual Machine-Chapter 2-thread security and lock optimization, and Chapter 4
Chapter 4 thread security and lock Optimization
Thread Security
Thread security in java
1 is unchangeable. The Immutable object must be thread-safe.
2. Absolute thread security: no matter what the runtime environment is, the caller does not need any additional synchronization measures. Generally, the caller must pay a large or even unrealistic price, mark itself as a thread-safe class in java APIs. Most of them are not absolute thread-safe.
3. Relatively thread-safe
4. The thread-compatible object itself is not thread-safe. However, you can ensure that the object can be safely used in the concurrent environment by correctly using Synchronization Methods on the caller.
5. Opposition to threads
Implementation of thread security
1 mutex Synchronization Murual Exclusion & Synchronization
A common mobile phone with concurrent correctness is guaranteed. Synchronization means that when multiple threads concurrently access Shared data, the shared data is saved and used by only one thread at a time.
The most basic mutex is the synchronized keyword.
You can also use the lock in the java. util. concurrent package to implement synchronous ReentrantLock,
JDK1.6 optimized synchronized, and the efficiency was basically the same as that of ReentrantLock.
2. Non-blocking Synchronization
Optimistic Concurrency strategy based on Conflict Detection
CAS operation Compare-and-Swap
3 No synchronization Solution
Lock Optimization
1. Spin locks and adaptive spin
2. Lock Elimination
3. Lock roughening
4. Magnitude lock
5. Biased lock
The entire book can be viewed in the Blog: deep understanding of Java Virtual Machine (VM) notes.