Home >Java >javaTutorial >What is a java lock?
Lock is the most important synchronization mechanism in Java concurrent programming. Locking is implemented in JAVA through the Synchronized keyword and related classes under the java.util.concurrent package.
The concept of lock in Java
Spin lock: means that when a thread acquires a lock, if the lock has been acquired by another thread, then the thread will Wait in a loop, and then continuously determine whether the lock can be successfully acquired. The loop will not exit until the lock is acquired.
Optimistic lock: Assume there is no conflict. When modifying the data, if it is found that the data is inconsistent with the previously acquired data, read the latest data and retry the modification after modification.
Pessimistic lock: Assume that concurrency conflicts will occur. , synchronize all related operations on data, and start locking when reading data
Exclusive lock (write): Add a write lock to the resource. The thread that owns the lock can modify the resource, and other threads cannot add more Lock (single write)
Shared lock (read): After adding a read lock to a resource, it can only be read but not modified. Other threads can only add read locks and cannot add write locks (multiple reads)
Reentrant lock: After a thread obtains a lock, it can freely enter the code synchronized with the same lock
Non-reentrant lock: After a thread obtains a lock, it cannot freely enter the same code Code to synchronize locks
Fair lock: The order of competing for locks is in the order of first come, first served
Unfair lock: The order of competing for locks is not in the order of first come, first served
Several important lock implementation methods in Java: synchronized, ReentrantLock, ReentrantReadWriteLock
Synchronization keywords: synchronized
Lock Scope: Object lock, class lock, distributed lock
synchronized features: Reentrant, exclusive, pessimistic lock
Lock optimization:
Lock elimination is a lock optimization method that occurs at the compiler level. It refers to the fact that when the virtual machine just-in-time compiler is running, it requires synchronization of some codes, but it is detected that there is no lock competition for shared data. Eliminate (parameters to enable lock elimination: -xx: DoEscapeAnalysis -XX: EliminateLocks)
Lock coarsening: In some cases, we want to merge many lock requests into one request to reduce shortcomings. Performance loss caused by a large number of lock requests, synchronization, and release within a period of time
Note: The synchronized keyword not only achieves synchronization, but JMM stipulates that synchronized must ensure visibility (cannot be cached)
synchronized usage code example:
public class Counter { private static int i = 0; // 等价于 synchronized(this) public synchronized void update() { i++; } public void updateBlock() { synchronized (this) { i++; } } // 等价于 synchronized (Counter.class) public static synchronized void staticUpdate() { i++; } public static void staticUpdateBlock() { synchronized (Counter.class) { i++; } } }
The above is the detailed content of What is a java lock?. For more information, please follow other related articles on the PHP Chinese website!