Java Performance
            artdb@ex-em.com | performeister.tistory.com | twitter @novathinker




Thread Synchronization
Java Performance
                                                                                     2

                 ...
Java Performance
         artdb@ex-em.com | performeister.tistory.com | twitter @novathinker




    Thread
Synchnonization
Java Performance
                                                                      4
                   Java Performan...
Java Performance
                                                                      5
                   Java Performan...
Java Performance
                                                                      6
                   Java Performan...
Java Performance
                                                                      7
                   Java Performan...
Java Performance
                                                                      8
                   Java Performan...
Java Performance
                                                                      9
                   Java Performan...
Java Performance
                                                                      10
                   Java Performa...
Java Performance
                                                                      11
                   Java Performa...
Java Performance
                                                                        12
                   Java Perfor...
Java Performance
         artdb@ex-em.com | performeister.tistory.com | twitter @novathinker




    Hotspot
Synchronizati...
Java Performance
                                                                                         14
             ...
Java Performance
                                                                      15
                   Java Performa...
Java Performance
                                                                                     16
                 ...
Java Performance
                                                                          17
                       Java ...
Java Performance
                                                                      18
                   Java Performa...
Java Performance
                                                                      19
                   Java Performa...
Java Performance
                                                                                  20
                    ...
Java Performance
                                                                      21
                   Java Performa...
Java Performance
                                                                      22
                   Java Performa...
Java Performance
                                                                      23
                   Java Performa...
Java Performance
         artdb@ex-em.com | performeister.tistory.com | twitter @novathinker




   IBM JVM
Synchronization
Java Performance
                                                                      25
                   Java Performa...
Java Performance
                        Java Performance Fundamental | twitter @novathinker
       IBM JVM Synchronizatio...
Java Performance
                   Java Performance Fundamental | twitter @novathinker
  IBM JVM Synchronization       ar...
Java Performance
                      Java Performance Fundamental | twitter @novathinker
     IBM JVM Synchronization   ...
Java Performance
                   Java Performance Fundamental | twitter @novathinker
  IBM JVM Synchronization       ar...
Java Performance
                   Java Performance Fundamental | twitter @novathinker
  IBM JVM Synchronization       ar...
Java Performance
                   Java Performance Fundamental | twitter @novathinker
  IBM JVM Synchronization       ar...
Java Performance
                   Java Performance Fundamental | twitter @novathinker
  IBM JVM Synchronization       ar...
Java Performance
                                                   33
Java Performance Fundamental | twitter @novathinker...
Upcoming SlideShare
Loading in...5
×

6장 Thread Synchronization

7,383

Published on

Java Performance Fundamental 세미나 자료입니다. 6장은 Thread Synchronization를 다루고 있습니다. 여기서는 Java에서 Thread라는 것은 어떻게 관리되어 왔으며 동기화는 어떤 역할을 하는지를 설명합니다. 더 나아가 JVM의 버전이 올라가면서 추가된 Hotspot JVM의 Biased Lock이나 IBM JVM의 Lock Reservation에 대해서도 설명하고 있습니다.

* Java의 Thread
* Java Synchronization
* Hotspot JVM Synchronization
* IBM JVM Synchronization

Published in: Technology
1 Comment
11 Likes
Statistics
Notes
No Downloads
Views
Total Views
7,383
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
102
Comments
1
Likes
11
Embeds 0
No embeds

No notes for slide
  • 6장 Thread Synchronization

    1. 1. Java Performance artdb@ex-em.com | performeister.tistory.com | twitter @novathinker Thread Synchronization
    2. 2. Java Performance 2 artdb@ex-em.com | performeister.tistory.com | twitter @novathinker Thread Synchronization 1) Java Thread 2) Thread Synchronization 3) Cooperation 4) Mutual execution 5) Light-Weight Lock Hotspot Synchronization 1) Light-Weight Lock 2) Biased Locking IBM JVM Synchronization 1) Thin Lock 2) Lock Reservation
    3. 3. Java Performance artdb@ex-em.com | performeister.tistory.com | twitter @novathinker Thread Synchnonization
    4. 4. Java Performance 4 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Thread Synchronization • Java Thread – Green Thread • JVM Created, scheduling Thread • 1.1 JVM Thread model • OS Thread n:1 • OS Thread • CPU – Native Thread • OS Kernel Thread • 1.3 Default
    5. 5. Java Performance 5 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Thread Synchronization • Thread Synchronization – Monitor : Java Synchronization Mechanism – Monitor • Cooperation – Class object wait(),notify() – Thread • Mutual Exclusion – Class class –
    6. 6. Java Performance 6 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Thread Synchronization • Cooperation – Cooperation • wait() Reader Thread Buffer empty Listen notify() Thread Buffer full
    7. 7. Java Performance 7 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Thread Synchronization • Cooperation – Monitor Monitor Critical Section Monitor suspend
    8. 8. Java Performance 8 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Thread Synchronization • Cooperation – Monitor
    9. 9. Java Performance 9 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Thread Synchronization • Mutual Exclusion – Shared Data • Runtime Data Area Data Thread • Heap  Instance Variables • Method Area  Class Variables • Object Class Monitor lock • Mutex object lock • Object Lock – Thread Object
    10. 10. Java Performance 10 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Thread Synchronization • Mutual Exclusion – Object Lock • Class file java.lang.class instance • Object lock object(instance) locking • Thread object locking • object lock counter – Lock count – Lock count – Count = 0 thread lock • Thread Critical Section lock
    11. 11. Java Performance 11 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Thread Synchronization • Mutual Exclusion – Critical Section • Critical Section – Synchronized statement – Synchronized method • Critical section object reference • Critical section instruction thread object lock • Critical section lock release • Object Lock critical section JVM
    12. 12. Java Performance 12 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Thread Synchronization • Light-Weight Lock – light-weight lock • JVM Light-Weight Lock • Object OS Mutex heavy- weight lock • locking No Atomic Operation Enter, Exit monitor Contended Heavy-Weight OS Lock Yes
    13. 13. Java Performance artdb@ex-em.com | performeister.tistory.com | twitter @novathinker Hotspot Synchronization
    14. 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. 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. 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. 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
    18. 18. Java Performance 18 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot Synchronization • Light-Weight Lock – Light-Weight Locking • Atomic Operation multiprocessor • lock Thread locking • Thread Lock • Thread lock –
    19. 19. Java Performance 19 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot Synchronization • Biased Locking – Java 6 default • Java 5 update 6 – Light Weight lock • Object lock thread lock • CAS – Biased lock • lock CAS • Thread (biased) lock CAS
    20. 20. Java Performance 20 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot Synchronization • Biased Locking Object Allocate Bias enabled Bias disabled Unlock Unlock 0 epoch Age 1 01 Hash Code Age 0 01 Recursive lock Initial lock Rebias Revoke bias Pointer to lock record 00 locked Thread ID epoch Age 1 01 Pointer to heavyweight monitor 10 Lock / Unlock
    21. 21. Java Performance 21 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot Synchronization • Biased Locking – Biased lock • Mark word tag 1 • 101  biased or biasable (thread id 0 unlocked) – Bias revocation • Lock Thread Thread lock light weight lock • Owner thread bias • Object mark word – Thread suspend  expensive
    22. 22. Java Performance 22 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot Synchronization • Biased Locking – Bulk Revocation • Class revocation • Class instance bias invalid – Bulk Rebiasing • Thread lock class • class instance bias invalid
    23. 23. Java Performance 23 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com Hotspot Synchronization • Biased Locking – Option • -XX:+UseBiasedLocking (Java5 update 6~) : biased lock java 6 default • -XX:BiasedLockingStartupDelay=0 (Java6~) : JVM Delay second • -XX:+TraceBiasedLocking (Java6~) : Biased Lock Debugging
    24. 24. Java Performance artdb@ex-em.com | performeister.tistory.com | twitter @novathinker IBM JVM Synchronization
    25. 25. Java Performance 25 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com IBM JVM Synchronization • Thin Lock – Thin lock Fat lock • Fat Lock : heavy-weight locking system • Thin Lock : Light-weight locking system – Object lock – Recursive lock • Lock default thin • Lock • IBM JDK 1.1.2
    26. 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. 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. 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
    29. 29. Java Performance Java Performance Fundamental | twitter @novathinker IBM JVM Synchronization artdb@ex-em.com | performeister.tistory.com 29 • Lock Reservation – Java 5 – Atomic Operation • 75% Lock thread • Performance – Idea • Object Lock (reserve) cost
    30. 30. Java Performance Java Performance Fundamental | twitter @novathinker IBM JVM Synchronization artdb@ex-em.com | performeister.tistory.com 30 • Lock Reservation – Object Layout • Object Header Lock Word • LRV bit : Lock reserved –1 Reserved –0 locking System LRV bit Thread ID Recursion Cnt* 1 (defined by base locking algorithm) 0
    31. 31. Java Performance Java Performance Fundamental | twitter @novathinker IBM JVM Synchronization artdb@ex-em.com | performeister.tistory.com 31 • Lock Reservation – Reserved mode • Reserve Thread ID • Recursion Count Lock Acquisition Thread ID 0 1 Reserved, Released Thread ID >0 1 Reserved , 0 0 1 Reserved
    32. 32. Java Performance Java Performance Fundamental | twitter @novathinker IBM JVM Synchronization artdb@ex-em.com | performeister.tistory.com 32 • Lock Reservation – Locking • Reserved mode atomic operation Recursion count • Thread Reservation cancel unreserve 0 0 1 Thread ID 0 1 aaaaaa 0 acquire release acquire unreserve Thread ID 1 1 bbbbbb 0 acquire release unreserve Thread ID 2 1 ccccccc 0 Reserved Mode Base Mode
    33. 33. Java Performance 33 Java Performance Fundamental | twitter @novathinker artdb@ex-em.com | performeister.tistory.com
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×