The difference between synchronized keyword and Lock in JUC, CAS operation and ABA problem

synchronized

The synchronized keyword will form monitorenter and monitorexit two bytecode instructions before and after the synchronization block after javac is compiled. The reference object is required to specify the locked and unlocked objects. If not explicitly specified, the method type (instance method or class method) of the synchronized modification is used to decide whether to take the object instance where the code is located or the Class object corresponding to the type as the thread. The lock held.

When executing the monitorenter instruction, first try to acquire the lock of the object. If the object is not locked, or the current thread already holds the lock of that object, the value of the lock counter is increased by one, and when the monitorexit instruction is executed, yes Decrease the value of the counter by one. When the value of the counter is zero, the lock is released. If the object lock fails, the current thread should be blocked and wait until the object that requested the lock is released by the thread that holds it.

A direct inference about synchronized :

  1. The synchronized block modified by synchronized is reentrant to the same thread. The same thread repeatedly enters the synchronized block without deadlock.
  2. The synchronized block modified by synchronized will unconditionally block the entry of other threads behind it before the thread holding the lock finishes executing and releases the lock. It cannot be forced to release the lock, nor can it interrupt the thread waiting for the lock.

JUC

After JDK5, the java.util.concurrent package (JUC) is newly provided in the Java class library, and the java.util.locks.Lock interface has become another mutually exclusive synchronization method for Java.
Null reentrant lock (ReentrantLock) is a common implementation class of the Lock interface. It adds some functions than synchronized:

  1. Waiting can be interrupted: When the thread holding the lock is not released for a long time, the waiting thread can choose to give up waiting.
  2. Fair lock: When multiple threads are waiting for the same lock, they must obtain the lock in sequence according to the time when the lock is applied for; Unfair lock: Any waiting thread has a chance to occupy the lock. ReentrantLock is a non-fair lock by default, which can be converted to a fair lock (boolean value) through the constructor. The use of a fair lock will reduce the performance of ReentrantLock and affect throughput.
  3. The lock binds multiple conditions: A ReentrantLock object can bind multiple Condition objects at the same time.

After JDK6 optimizes synchronized, the performance of synchronized and ReentrantLock is the same .

The difference between sychronized and Lock

  • Synchronized is synchronization at the Java syntax level, and Lock is an interface.
  • Lock needs to release the lock manually, otherwise it will cause deadlock, and synchronized will release automatically
  • The Java virtual machine can record information about locks in synchronized in the metadata of threads and objects, but Lock is difficult to know.

CAS guarantees the atomicity of operations

CAS (Compare and Swap): The CAS instruction requires three operands, which are the memory address V, the old expected value A, and the new value B to be set.
When the CAS instruction is executed, if and only if V matches A, the processor will update the value of V with B, otherwise it will not perform the update and will not be interrupted by other threads during execution.

CAS vulnerability: ABA problem

ABA problem: When testing whether V meets A, A may be modified by other threads (from A to B and then from B to A), but CAS cannot find it, and it will continue to execute. In most cases, ABA problems will not affect the correctness of program concurrency.

Guess you like

Origin blog.csdn.net/weixin_43663421/article/details/109351067