SlideShare ist ein Scribd-Unternehmen logo
1 von 39
Downloaden Sie, um offline zu lesen
Java Concurrency #2
                             Sergey Lukjanov, 2012
                           slukjanov@mirantis.com




Sunday, November 4, 12
JMM / what & why?




Sunday, November 4, 12
JMM / what & why?
          • JVM observes within-thread as-if-serial semantics;




Sunday, November 4, 12
JMM / what & why?
          • JVM observes within-thread as-if-serial semantics;
          • JMM defines how threads interact through memory;




Sunday, November 4, 12
JMM / what & why?
          • JVM observes within-thread as-if-serial semantics;
          • JMM defines how threads interact through memory;
          • JMM answers the main question:
             • “When the thread will see changes done in the
                         other one?”




Sunday, November 4, 12
JMM / what & why?
          • JVM observes within-thread as-if-serial semantics;
          • JMM defines how threads interact through memory;
          • JMM answers the main question:
             • “When the thread will see changes done in the
                         other one?”


          • why we need it?
            • write correct applications;
            • avoid over synchronizations.


Sunday, November 4, 12
Variable visibility
                                   x = y = 0

                                Thread 1 Thread 2
                                a = x     b = y
                                y = 1     x = 1


                                     a = ?
                                     b = ?




Sunday, November 4, 12
Variable visibility
                                    x = y = 0

                                Thread 1 Thread 2
                                a = x        b = y
                                y = 1        x = 1

                                a   =   0;   b   =   0;
                                a   =   0;   b   =   1;
                                a   =   1;   b   =   0;
                                a   =   1;   b   =   1;


Sunday, November 4, 12
Reordering




Sunday, November 4, 12
Reordering
          • compiler can optimize ops order (w/o changing
               application within-thread as-if-serial semantics);




Sunday, November 4, 12
Reordering
          • compiler can optimize ops order (w/o changing
               application within-thread as-if-serial semantics);
          •    CPU can execute commands in the other order in
               some cases;




Sunday, November 4, 12
Reordering
          • compiler can optimize ops order (w/o changing
               application within-thread as-if-serial semantics);
          •    CPU can execute commands in the other order in
               some cases;
          •    cache can store values back to the main memory in
               not the same order as it was written by application;




Sunday, November 4, 12
Reordering
          • compiler can optimize ops order (w/o changing
               application within-thread as-if-serial semantics);
          •    CPU can execute commands in the other order in
               some cases;
          •    cache can store values back to the main memory in
               not the same order as it was written by application;
          • etc.




Sunday, November 4, 12
Happens-before relationship




Sunday, November 4, 12
Happens-before relationship
          • it is a guarantee that memory written to by statement
               A is visible to statement B, that is, that A completes
               its write before statement B starts its read;




Sunday, November 4, 12
Happens-before relationship
          • it is a guarantee that memory written to by statement
               A is visible to statement B, that is, that A completes
               its write before statement B starts its read;
          •    A happens-before B := hb(A, B);




Sunday, November 4, 12
Happens-before relationship
          • it is a guarantee that memory written to by statement
               A is visible to statement B, that is, that A completes
               its write before statement B starts its read;
          •    A happens-before B := hb(A, B);
          •    transitive: hb(x, y) & hb (y, z) => hb(x, z).




Sunday, November 4, 12
Will T2 see any changes done by T1? #1
                          Thread 1              Thread 2
                             ⇓                     ⇓
                           y = 2                lock M2
                             ⇓                     ⇓
                          lock M1              temp = x
                             ⇓                     ⇓
                           x = 1               unlock M2
                             ⇓                     ⇓
                         unlock M1             temp2 = y
                             ⇓                     ⇓
                           z = 3               temp3 = z
                             ⇓                     ⇓

Sunday, November 4, 12
Will T2 see any changes done by T1? #2
                          Thread 1              Thread 2
                             ⇓                     ⇓
                           y = 2                lock M1
                             ⇓                     ⇓
                          lock M1              temp = x
                             ⇓                     ⇓
                           x = 1               unlock M1
                             ⇓                     ⇓
                         unlock M1             temp2 = y
                             ⇓                     ⇓
                           z = 3               temp3 = z
                             ⇓                     ⇓

Sunday, November 4, 12
Will T2 see any changes done by T1? #3
                      Thread 1                  Thread 2
                         ⇓                         ⇓
                       y = 2                    read M1
                         ⇓                         ⇓
                    volatile M1                temp = x
                         ⇓                         ⇓
                       x = 1                   temp2 = y
                         ⇓                         ⇓
                     write M1                  temp3 = z
                         ⇓                         ⇓
                       z = 3
                         ⇓

Sunday, November 4, 12
Will T2 see any changes done by T1? #4
                          Thread 1              Thread 2
                             ⇓                     ⇓
                           y = 2           if(!t1.isAlive())
                             ⇓                     ⇓
                          lock M1              temp = x
                             ⇓                     ⇓
                           x = 1               temp2 = y
                             ⇓                     ⇓
                         unlock M1             temp3 = z
                             ⇓                     ⇓
                           z = 3
                             ⇓

Sunday, November 4, 12
Will T2 see any changes done by T1? #5
                 Thread 1                      Thread 2
                    ⇓                             ⇓
                  y = 2                  Thread.interrupted()
                    ⇓                             ⇓
                  x = 1                        temp = x
                    ⇓                             ⇓
              t2.interrupt()                  temp2 = y
                    ⇓                             ⇓
                  z = 3                       temp3 = z
                    ⇓                             ⇓




Sunday, November 4, 12
Happens-before rules




Sunday, November 4, 12
Happens-before rules
          • each action A in thread happens-before each action B
               (that placed after A in code) in the same thread;




Sunday, November 4, 12
Happens-before rules
          • each action A in thread happens-before each action B
               (that placed after A in code) in the same thread;
          • unlock monitor happens-before any following lock on
               the same monitor;




Sunday, November 4, 12
Happens-before rules
          • each action A in thread happens-before each action B
               (that placed after A in code) in the same thread;
          • unlock monitor happens-before any following lock on
               the same monitor;
          • write into volatile var happens-before any following
               read from the same volatile var;




Sunday, November 4, 12
Happens-before rules
          • each action A in thread happens-before each action B
               (that placed after A in code) in the same thread;
          • unlock monitor happens-before any following lock on
               the same monitor;
          • write into volatile var happens-before any following
               read from the same volatile var;
          • `Thread.start()` happens-before first action in the
               started thread;




Sunday, November 4, 12
Happens-before rules
          • each action A in thread happens-before each action B
               (that placed after A in code) in the same thread;
          • unlock monitor happens-before any following lock on
               the same monitor;
          • write into volatile var happens-before any following
               read from the same volatile var;
          • `Thread.start()` happens-before first action in the
               started thread;
          • last action in thread1 happens-before any action in
               thread2 if thread2 detects that thread1 ends by
               invoking `thread1.join()` or if `thread1.isAlive()`
               invoked and `false` returned;

Sunday, November 4, 12
Happens-before rules #2




Sunday, November 4, 12
Happens-before rules #2
          • constructor happens-before finalizer;




Sunday, November 4, 12
Happens-before rules #2
          • constructor happens-before finalizer;
          • thread interruption happens-before place, where
               thread detects that it has been interrupted;




Sunday, November 4, 12
Happens-before rules #2
          • constructor happens-before finalizer;
          • thread interruption happens-before place, where
               thread detects that it has been interrupted;
          • some more rules...




Sunday, November 4, 12
Reordering: synchronized


                            ...
                            synchronized (this) {
                              ...
                              ...
                            } // end of synch
                            ...




Sunday, November 4, 12
Reordering: volatile

                               ...
                               volatile write
                               ...

                               ...
                               volatile read
                               ...




Sunday, November 4, 12
Volatile guarantees




Sunday, November 4, 12
Volatile guarantees
          • happens-before;




Sunday, November 4, 12
Volatile guarantees
          • happens-before;
          • reordering;




Sunday, November 4, 12
Volatile guarantees
          • happens-before;
          • reordering;
          • atomic read/write (including Long and Double at i386)




Sunday, November 4, 12
Examples




Sunday, November 4, 12

Weitere ähnliche Inhalte

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Courses: concurrency #2

  • 1. Java Concurrency #2 Sergey Lukjanov, 2012 slukjanov@mirantis.com Sunday, November 4, 12
  • 2. JMM / what & why? Sunday, November 4, 12
  • 3. JMM / what & why? • JVM observes within-thread as-if-serial semantics; Sunday, November 4, 12
  • 4. JMM / what & why? • JVM observes within-thread as-if-serial semantics; • JMM defines how threads interact through memory; Sunday, November 4, 12
  • 5. JMM / what & why? • JVM observes within-thread as-if-serial semantics; • JMM defines how threads interact through memory; • JMM answers the main question: • “When the thread will see changes done in the other one?” Sunday, November 4, 12
  • 6. JMM / what & why? • JVM observes within-thread as-if-serial semantics; • JMM defines how threads interact through memory; • JMM answers the main question: • “When the thread will see changes done in the other one?” • why we need it? • write correct applications; • avoid over synchronizations. Sunday, November 4, 12
  • 7. Variable visibility x = y = 0 Thread 1 Thread 2 a = x b = y y = 1 x = 1 a = ? b = ? Sunday, November 4, 12
  • 8. Variable visibility x = y = 0 Thread 1 Thread 2 a = x b = y y = 1 x = 1 a = 0; b = 0; a = 0; b = 1; a = 1; b = 0; a = 1; b = 1; Sunday, November 4, 12
  • 10. Reordering • compiler can optimize ops order (w/o changing application within-thread as-if-serial semantics); Sunday, November 4, 12
  • 11. Reordering • compiler can optimize ops order (w/o changing application within-thread as-if-serial semantics); • CPU can execute commands in the other order in some cases; Sunday, November 4, 12
  • 12. Reordering • compiler can optimize ops order (w/o changing application within-thread as-if-serial semantics); • CPU can execute commands in the other order in some cases; • cache can store values back to the main memory in not the same order as it was written by application; Sunday, November 4, 12
  • 13. Reordering • compiler can optimize ops order (w/o changing application within-thread as-if-serial semantics); • CPU can execute commands in the other order in some cases; • cache can store values back to the main memory in not the same order as it was written by application; • etc. Sunday, November 4, 12
  • 15. Happens-before relationship • it is a guarantee that memory written to by statement A is visible to statement B, that is, that A completes its write before statement B starts its read; Sunday, November 4, 12
  • 16. Happens-before relationship • it is a guarantee that memory written to by statement A is visible to statement B, that is, that A completes its write before statement B starts its read; • A happens-before B := hb(A, B); Sunday, November 4, 12
  • 17. Happens-before relationship • it is a guarantee that memory written to by statement A is visible to statement B, that is, that A completes its write before statement B starts its read; • A happens-before B := hb(A, B); • transitive: hb(x, y) & hb (y, z) => hb(x, z). Sunday, November 4, 12
  • 18. Will T2 see any changes done by T1? #1 Thread 1 Thread 2 ⇓ ⇓ y = 2 lock M2 ⇓ ⇓ lock M1 temp = x ⇓ ⇓ x = 1 unlock M2 ⇓ ⇓ unlock M1 temp2 = y ⇓ ⇓ z = 3 temp3 = z ⇓ ⇓ Sunday, November 4, 12
  • 19. Will T2 see any changes done by T1? #2 Thread 1 Thread 2 ⇓ ⇓ y = 2 lock M1 ⇓ ⇓ lock M1 temp = x ⇓ ⇓ x = 1 unlock M1 ⇓ ⇓ unlock M1 temp2 = y ⇓ ⇓ z = 3 temp3 = z ⇓ ⇓ Sunday, November 4, 12
  • 20. Will T2 see any changes done by T1? #3 Thread 1 Thread 2 ⇓ ⇓ y = 2 read M1 ⇓ ⇓ volatile M1 temp = x ⇓ ⇓ x = 1 temp2 = y ⇓ ⇓ write M1 temp3 = z ⇓ ⇓ z = 3 ⇓ Sunday, November 4, 12
  • 21. Will T2 see any changes done by T1? #4 Thread 1 Thread 2 ⇓ ⇓ y = 2 if(!t1.isAlive()) ⇓ ⇓ lock M1 temp = x ⇓ ⇓ x = 1 temp2 = y ⇓ ⇓ unlock M1 temp3 = z ⇓ ⇓ z = 3 ⇓ Sunday, November 4, 12
  • 22. Will T2 see any changes done by T1? #5 Thread 1 Thread 2 ⇓ ⇓ y = 2 Thread.interrupted() ⇓ ⇓ x = 1 temp = x ⇓ ⇓ t2.interrupt() temp2 = y ⇓ ⇓ z = 3 temp3 = z ⇓ ⇓ Sunday, November 4, 12
  • 24. Happens-before rules • each action A in thread happens-before each action B (that placed after A in code) in the same thread; Sunday, November 4, 12
  • 25. Happens-before rules • each action A in thread happens-before each action B (that placed after A in code) in the same thread; • unlock monitor happens-before any following lock on the same monitor; Sunday, November 4, 12
  • 26. Happens-before rules • each action A in thread happens-before each action B (that placed after A in code) in the same thread; • unlock monitor happens-before any following lock on the same monitor; • write into volatile var happens-before any following read from the same volatile var; Sunday, November 4, 12
  • 27. Happens-before rules • each action A in thread happens-before each action B (that placed after A in code) in the same thread; • unlock monitor happens-before any following lock on the same monitor; • write into volatile var happens-before any following read from the same volatile var; • `Thread.start()` happens-before first action in the started thread; Sunday, November 4, 12
  • 28. Happens-before rules • each action A in thread happens-before each action B (that placed after A in code) in the same thread; • unlock monitor happens-before any following lock on the same monitor; • write into volatile var happens-before any following read from the same volatile var; • `Thread.start()` happens-before first action in the started thread; • last action in thread1 happens-before any action in thread2 if thread2 detects that thread1 ends by invoking `thread1.join()` or if `thread1.isAlive()` invoked and `false` returned; Sunday, November 4, 12
  • 30. Happens-before rules #2 • constructor happens-before finalizer; Sunday, November 4, 12
  • 31. Happens-before rules #2 • constructor happens-before finalizer; • thread interruption happens-before place, where thread detects that it has been interrupted; Sunday, November 4, 12
  • 32. Happens-before rules #2 • constructor happens-before finalizer; • thread interruption happens-before place, where thread detects that it has been interrupted; • some more rules... Sunday, November 4, 12
  • 33. Reordering: synchronized ... synchronized (this) { ... ... } // end of synch ... Sunday, November 4, 12
  • 34. Reordering: volatile ... volatile write ... ... volatile read ... Sunday, November 4, 12
  • 36. Volatile guarantees • happens-before; Sunday, November 4, 12
  • 37. Volatile guarantees • happens-before; • reordering; Sunday, November 4, 12
  • 38. Volatile guarantees • happens-before; • reordering; • atomic read/write (including Long and Double at i386) Sunday, November 4, 12