In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Java object structure and object lock example analysis, the article introduces in great detail, has a certain reference value, interested friends must read it!
1. Java object structure
The Java object structure consists of three parts: object header, object body, and padding bytes, as shown in the figure:
The object header also includes three fields:
The first field, called Mark Word, stores its own runtime data, such as GC flag bits, hash codes, lock status, and so on.
The second field, called Class Pointer (class object pointer), holds the address of the Class object in the method area, which is used by the virtual machine to determine which class instance the object is.
The third field is called Array Length (array length). If the object is a Java array, then this field must have data for recording the length of the array; if the object is not an Java array, then this field does not exist, so this is an optional field.
In 32-bit JVM virtual machines, both Mark Word and Class Pointer are 32-bit; in 64-bit JVM virtual machines, Mark Word and Class Pointer are both 64-bit. What we need to focus on is mark word, because it has something to do with the underlying principles of synchronized.
2. Structure information of Mark Word
There are four states of Java built-in locks, with the following levels from low to high: no lock, bias lock, lightweight lock and heavy lock. In fact, before JDK 1.6, Java built-in lock was still a heavy lock, which was relatively inefficient. After JDK 1.6, in order to improve the efficiency of lock acquisition and release, JVM optimized the implementation of synchronized, introducing biased lock and lightweight lock. From then on, there are four kinds of Java built-in lock, and the four states will gradually upgrade with the competition, and it is an irreversible process. That is, it cannot be degraded, that is, only lock upgrades can be carried out.
1. Mark Word field structure under different lock states:
The structure of the Mark Word field is strongly related to the state of the Java built-in lock. In order for the Mark Word field to store more information, JVM sets the lowest two bits of Mark Word to the Java built-in lock state bits. The 32-bit Mark Word structure under different lock states is shown in the table:
The structure of 64-bit Mark Word is similar to that of 32-bit Mark Word, as shown in the table:
2. Introduction of 64-bit Mark Word:
Since the mainstream JVM is 64-bit, we use 64-bit Mark Word. Next, the contents of each part of the 64-bit Mark Word are introduced in detail.
(1) lock: lock status marker bits, which occupy two binary bits. Because you want to use as few binary bits as possible to represent as much information as possible, the lock flag is set. If the value of the tag is different, the meaning of the entire Mark Word is different.
(2) biased_lock: whether the object enables the bias lock flag, which occupies only one binary bit. A value of 1 indicates that the object has a bias lock enabled, and a value of 0 indicates that the object has no bias lock. The combination of the lock and biased_lock tag bits together indicates what kind of locked state the Object instance is in. The meaning of the combination of the two is shown in Table 2-3.
The combination of the lock and biased_lock tag bits together indicates what kind of locked state the Object instance is in. The meaning of the combination of the two is shown in the table:
(3) ptr_to_lock_record: 62-bit pointer to the lock record in the stack frame in a lightweight lock state.
(4) ptr_to_heavyweight_monitor: 62-bit pointer to the object monitor in a heavyweight lock.
3. No lock, bias lock, lightweight lock and heavy lock
Prior to JDK 1.6, all Java built-in locks were heavyweight locks. Heavyweight locks will cause CPU to switch frequently between user mode and kernel state, so the cost is high and the efficiency is low. In order to reduce the performance consumption caused by acquiring and releasing locks, JDK version 1.6 introduces the implementation of biased locks and lightweight locks. Therefore, there are four states of built-in lock in JDK version 1.6: no lock state, partial lock state, lightweight lock state, and heavyweight lock state, which escalate gradually with the competition. Built-in locks can be upgraded but cannot be degraded, which means that biased locks can no longer be downgraded to lightweight locks. The purpose of this strategy, which can be upgraded but not degraded, is to improve the efficiency of acquiring and releasing locks.
(1) unlocked status:
When the Java object was first created, there was no thread to compete, indicating that the object was in a lock-free state (wireless programs competed for it), so the lock identifier bit was 0 and the lock state was 01.
(2) bias lock state:
Biased lock means that a piece of synchronization code has been accessed by the same thread, then the thread will automatically acquire the lock, reducing the cost of acquiring the lock. If the built-in lock is biased, when a thread competes for the lock, the bias lock is used first to indicate that the built-in lock favors the thread, and the thread does not need to do any checking and switching when it wants to execute the synchronization code associated with the lock. The bias lock is very efficient when the competition is not fierce.
The Mark Word that favors the lock state records the ID of the thread preferred by the built-in lock, and the built-in lock treats the thread as its own acquaintance. In this case, the preferred lock identifier bit is 1 and the lock state is 01.
(3) lightweight lock status:
When two threads compete for the lock object, the situation changes. The lock is no longer biased (exclusive), the lock is upgraded to a lightweight lock, and the two threads compete fairly. Which thread occupies the lock object first, the Mark Word of the lock object points to the lock record in the stack frame of the thread. At this time, the bias lock identification bit is 0, and the lock state is 00.
When the lock is in a bias lock and is preempted by another thread, the bias lock is upgraded to a lightweight lock. The thread that attempts to preempt will attempt to acquire the lock in the form of spin without blocking the lock-grabbing thread to improve performance.
The spin principle is very simple. If the thread holding the lock can release the lock resource in a very short time, then the thread waiting for the competitive lock does not need to switch between kernel state and user state to enter the blocking pending state. They just need to wait (spin), and the thread holding the lock can acquire the lock immediately after releasing the lock, thus avoiding the consumption of user thread and kernel switching.
However, thread spin needs to consume CPU, and if you can't get the lock all the time, then the thread can't always occupy CPU spin to do useless work, so you need to set a maximum spin waiting time. For the choice of spin period in JVM, adaptive spin lock is introduced after JDK 1.6. adaptive spin lock means that the spin time is not fixed, but is determined by the spin time of the previous time on the same lock and the state of the lock owner. If the thread spins successfully, there will be more spins next time, and if the spins fail, the number of spins will decrease.
If the thread holding the lock executes longer than the maximum spin waiting time and still does not release the lock, it will cause other threads contending for the lock to fail to acquire the lock within the maximum waiting time, and the spin will not last forever. At this point, the contending thread stops spinning and enters a blocking state, and the lock expands into a heavy lock.
(4) weight lock status:
Heavyweight locks can cause blocking between other requesting threads and slow performance. The heavyweight lock is also called a synchronous lock, and the lock object MarkWord changes again to point to a monitor object that registers and manages queued threads in the form of a collection. At this time, the bias lock identification bit is 0, and the lock state is 10.
The above is all the content of the article "sample Analysis of Java object structure and object Lock". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.