The document discusses thread synchronization in Java. It covers Java threads, synchronization mechanisms like monitors and locks, cooperation between threads using wait() and notify(), and mutual exclusion with synchronized blocks and methods. It also describes lightweight locking techniques used in Hotspot like biased locking, and thin locks and lock reservation in IBM's JVM.
14. Java Performance
14
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot Synchronization
• Light-Weight Lock
– Object Header
• Object Header word Mark Work
Mark Word Hash Code (0) Age Biased Tag
Thread ID, epoch(1)
23 bit 6 bit 1 bit 2 bit
Hash Code (0) Age Biased Tag Biased bit Tag
Thread ID, epoch(1)
0 01 Unlocked
(000) Lock Record Address 0 00 Light-weight locked
(010) Monitor Address 0 10 Heavy-weight locked
0 11 Marked for GC
(011) Forwarding Address 1 01 Biased / Biasable
15. Java Performance
15
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot Synchronization
• Light-Weight Lock
– Light-Weight Locking
• Object Header word Mark Work
• Atomic compare-and-swap(CAS) instruction
lock
• CAS thread lock
• CAS heavy weight lock
• Object lock thread stack frame mark
word move (displaced mark )
• Lock Mark Word
16. Java Performance
16
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot Synchronization
• Light-Weight Lock
– Light-Weight Locking
Execution Execution
Stack Stack
Locking
Method Method
Activation Activation
Mark word 01 Stack pointer
Lock Displaced hdr Mark word 01
Record Object Object
owner owner
17. Java Performance
17
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot Synchronization
• Light-Weight Lock
– Light-Weight Locking
Unlock
Hash Code Age 0 01
Recursive lock
Light weight lock Pointer to lock record 00 Unlock
heavy weight lock Pointer to heavyweight 10
26. Java Performance
Java Performance Fundamental | twitter @novathinker
IBM JVM Synchronization artdb@ex-em.com | performeister.tistory.com 26
• Thin Lock
– Object Layout for Thin Lock
• Lock Word
Garbage Collection
(2bits) bit (2bits)
Pinned
Object Size G P
(double word) Array bit (1bit)
Class pointer or array size
Monitor Shape bit Thread index count
(1bit) S H T A
Monitor index Type
Hash code state (2bits)
Object Data
Hash code
27. Java Performance
Java Performance Fundamental | twitter @novathinker
IBM JVM Synchronization artdb@ex-em.com | performeister.tistory.com27
• Thin Lock
– Object Layout for Thin Lock
• Object Lock Word 24 bit
• Lock Word bit Lock
– Monitor Shape Lock
– 0 Thin lock
– 1 Fat lock
• Thin Lock •Recursive (nested) lock count
•Maximum 255 nested lock
Count
0 Thread ID (15 bits)
(8bits)
28. Java Performance
Java Performance Fundamental | twitter @novathinker
IBM JVM Synchronization artdb@ex-em.com | performeister.tistory.com 28
• Thin Lock
Spin
– Thin Locking lock
loop
Fail Own No Get
CAS
Lock? Lock? No
Success YES YES
0 Nested Lock Lock
24bit monitor shape, Count
count 0 Lock Release Fat lock
Thread ID Monitor index monitor
Count
Lock Release index
Lock word
CAS Monitor shape 1