Lock principle
java object structure
java object instance includes an object header and data, wherein the object header 12B, which are aligned with bits 4B
Object header (12B)
It contains mark word and klass pointer
- mark word includes lock information, hashcode, gc information (64bit, 8B)
- klass pointer pointer specified object meta data (32bit, 4B)
makr word
- 7B for storing the information hashcode
- The first byte 8 store generational age biased locking status information and the object
1bit not used | 3bit generational Age | 1bit biased locking flag | 1bit object status |
---|
- No lock state
if the single-threaded, and there is no synchronization block locking or manually, with no lock state - Biased locking
when only one thread synchronization code to block calls, it will tend to enter the locked state
but because of biased locking delay , or no lock state may sometimes
exist biased locking delay is due to start at the Jvm, there will be a lot of resources, and most requires heavyweight lock is constantly biased very wasteful from the release lock, so lock biased delay (approximately 4000 milliseconds), by XX: + UseBiasedLocking --XX: BiasedLockingStartupDelay = 0 to turn off the biased locking delayed
biased locking bias situation only once, but to eliminate biased locking the same batch of re-add a class lightweight lock, jvm will be re-biased
after exit biased locking synchronized block or biased locking - Lightweight locks
the exit lock lightweight sync block are restored with no lock state