1. Biased lock: the biased lock mode is used to bias the lock to a given lock thread. When this mode is set to the lower three bits, the lock may be biased toward a particular thread or "anonymous", indicating that it may be biased. When a lock is biased toward a given thread, locking and unlocking can be performed by that thread without using atomic operations. When the lock deviation is revoked, it will return to the normal state as described below. Note: Hotspot does not enable biased locking objects for the first few seconds (currently four seconds) of JVM startup. This is because some benchmarks and NetBeans have a lot of thread contention during startup, and the cancellation cost is very high. You can set the shutdown delay startup - XX:BiasedLockingStartupDelay=0 in ieda, as shown in the figure below. If you call the calculation hashcode() method lock, you will lose the thread bias feature.
2. Lightweight lock: lightweight lock is upgraded to lightweight lock through lock inflation process when there is no lock competition in single thread or multi thread alternate execution. To get the cup, we need not call the os function, but the lock function implemented by the jvm itself. Lightweight locks try to solve the problem of thread synchronization at the application level without triggering the mutually exclusive operation of the operating system. Lightweight locks reduce the probability of multiple threads entering the mutually exclusive operation, which can not replace the mutually exclusive operation, and the efficiency is slower than biased locks.
3. Heavyweight lock: in the process of multi-threaded competitive lock, lock inflation is upgraded to heavyweight lock. Each time lock is acquired, os lock function is called, including lock release, which is slower than lightweight lock. If you call the wait() method, the lock immediately becomes a heavyweight lock.
1, First we need to know what is a java object header?
Then go to the hotpot(jvm) directory of the jdk source code to know the figure. There is a comment in the source code of markOop. openJdk Source code (you may need VPN if you can't open it.).
Low 3-digit mark of each lock
1. It means that the java object header will have different forms in different states of the object. There are mainly three states: unlocked state, locked state and gc Tagged state. Then I can understand that the lock taking in ava is actually to lock the object, that is, to change the state of the object header. If the lock is successful, the synchronization code block will be entered. However, there are many types of locks in java. From the above figure, we can see that there are three lock states: biased lock, lightweight lock and weight lock. The efficiency of these three kinds of locks is totally different. The analysis of efficiency will be analyzed in the following section. We can only make reasonable use of locks with reasonable design code. So what are the principles of these three locks? So we need to study this object first
Don't talk too much nonsense
*Introduce dependency package
<dependency> <groupId>org.openjdk.jol</groupId> <artifactId>jol-core</artifactId> <version>0.8</version> </dependency>
import org.openjdk.jol.info.ClassLayout; import static java.lang.System.out; public class JOLExample7 { static B b; public static void main(String[] args) throws Exception { // Thread.sleep(10000); b = new B(); out.println("befre lock"); out.println(ClassLayout.parseInstance(b).toPrintable());//No lock Thread t1= new Thread(){ public void run() { synchronized (b){ try { Thread.sleep(5000); System.out.println("t1 release"); } catch (InterruptedException e) { e.printStackTrace(); } } } }; t1.start(); Thread.sleep(1000); out.println("t1 lock ing"); out.println(ClassLayout.parseInstance(b).toPrintable());//Lightweight lock sync(); out.println("after lock"); out.println(ClassLayout.parseInstance(b).toPrintable());//Weight lock System.gc(); out.println("after gc()"); out.println(ClassLayout.parseInstance(b).toPrintable());//Weight lock -- gc } public static void sync() throws InterruptedException { synchronized (b){ System.out.println("t1 main lock"); out.println(ClassLayout.parseInstance(b).toPrintable());//Weight lock } } }
public class B { }
Execution result:
Performance comparison of each blog is not demonstrated one by one: biased lock > lightweight lock > heavyweight lock