SlideShare a Scribd company logo
Concurrency: Best Practices




Pramod B Nagaraja
IBM India Pvt LTD                       1
Agenda

    Why Concurrency?

    Concurrency Panoramas

    Synchronization
       When and What to synchronize

    Guidelines for designing Concurrent Applications

    Excessive Synchronization

    Synchronization Optimization

    Latches & Barriers

                                                       2
1.0 Why Concurrency
Story so far….


    Most of the applications ran on system with few
    processors

    Relied on faster hardware, rather than
    Software Concurrency/Parallelism, for better
    performance

                                                   3
1.0 Why Concurrency
…Current scenario


    Moore's Law predicts that the number of
    transistors on a chip doubles every two years

    Faster Dual core machines demand
    Concurrency

    Multithreading is the pulse of Java
                                                    4
2.O Concurrency Panoramas
• Atomicity
  – Certain pieces of an application must all be
    executed as one unit




                                                   5
2.O Concurrency Panoramas

    Atomicity
    synchronized int getBalance() {
         return balance;
    }
        synchronized void setBalance(int x) {
         balance = x;}
    void deposit(int x) {              void withdraw(int x) {
         int b = getBalance();           int b = getBalance();
         setBalance(b + x);}             setBalance(b - x);}}
                                                                 6
2.O Concurrency Panoramas

    Visibility
     −   Changes that you make to a value to be
         visible precisely when you intend them to be




                                                        7
2.O Concurrency Panoramas

    Visibility
class LoopMayNeverEnd {
    boolean done = false;
    void work() {           void stopWork() {
      while (!done) {            done = true; }
                            }
           // do work
       }
}
                                                  8
3.0 Synchronization


Things which matter MOST should never be at
      mercy of things which matter LEAST




                                              9
3.1 When to Synchronize
Pitfalls when synchronizing:
     Forgetting to synchronize a resource that
      should be synchronized
     Synchronizing the wrong resource
     Synchronizing too often



                                                  10
3.2 What to Synchronize

    Not just the resource, but also the Transaction
    involving the resource

    Pay attention to Data integrity at multiple
    levels of granularity




                                                  11
3.2 What to Synchronize
private int foo;
  public synchronized int getFoo() {
  return foo;
  }
  public synchronized void setFoo(int f) {
  foo = f;
  }
  setFoo(getFoo() + 1); //not thread safe
                                             12
4. Guidelines for designing
          Concurrent Applications

    4 common concurrency mechanisms generally
    used in combination with each other to safely
    access data from multiple threads
    −   Dynamic Exclusion Through Implicit Locks
    −   Structural Exclusion Through Confinement
    −   Immutability
    −   Cooperation

                                                   13
4.1 Dynamic Exclusion Through
        Implicit Locks
Lock associated with an object is acquired when a
method is declared synchronized.
   public synchronized void setName(String name);


    Limitation
     −   When synchronization is used inappropriately or
         excessively it causes poor performance and
         deadlock.



                                                           14
4.1 Dynamic Exclusion Through
            Implicit Locks

    Best Practice Tip
       Do not perform CPU intensive and I/O operations inside
        synchronized method.
       When possible synchronize on block of code instead of
        synchronizing entire method.
        Ex: synchronize(lock) { //critical section guarded by lock}




                                                                      15
4.2 Structural Exclusion Through
              Confinement

    Thread confinement : Access object from a single
    thread using thread-per-session approach

    Instance confinement : Use encapsulation
    techniques to restrict access to object state from
    multiple threads

    Method confinement : Do not allow an object to
    escape from method by referencing it through local
    variables

                                                         16
4.2 Structural Exclusion Through
              Confinement

    Limitations :
     −   Confinement cannot be relied on unless a design
         is leak proof

    Best Practice Tip :
      Combine        confinement with appropriate locking
         discipline
      Use   ThreadLocal variables to maintain Thread
         Confinement
                                                             17
4.3 Immutability

    Immutable objects do not need additional
    synchronization
                                   afe {
    Public final class ImmutableClass
                            ea d-s
      private final intThr = 100;
                        field1
                   ly
               ent String field2=“FIN”;
      private final
       In her
    ………..
    }
                                               18
4.3 Immutability

    Limitation
    −   In a software application only some classes can
        be immutable.

    Best Practice Tip
    −   Even when class is not fully immutable, declare
        its non changeable fields as final to limit its
        mutability.


                                                          19
4.4 Cooperation
What is purpose of wait-and-notify mechanism?
When one thread needs a certain condition to exist and
another thread can create that condition then we use wait
and notify methods.
                                 Thread 1

              Thread 2               CONDITION MET
                                     STOP!! CONDITION NOT
                                     MET
                                     Thread 1 can
                                     proceed cannot progress
                                     Thread 1

 Thread 2 - “I will meet
 your condition.”

                                                               20
4.4 Cooperation
Limitation
   − When there are multiple waiting threads, you cannot be
     certain which thread is woken up.
   − Due to race conditions there could be missed or early
     notifications.
Best Practice Tip
 Safe to wake up all the threads using notifyAll() method instead
  of waking an arbitrary thread using notify()
 Sometimes condition that caused thread to wait is not achieved
  when wait() returns, therefore put wait() call in a while loop.
 Wait() and notify() must always be called inside synchronized
  code.
                                                                    21
5. Excessive Synchronization

    Poor performance

    Deadlock
       Can occur when multiple threads each acquire
        multiple locks in different orders




                                                       22
5.1 Deadlock
public static Object cacheLock = new Object();
 public static Object tableLock = new Object();
 ...
 public void oneMethod() {          public void anotherMethod() {
  synchronized (cacheLock) {          synchronized (tableLock) {
                                       synchronized (cacheLock) {
       synchronized (tableLock) {        doSomethingElse();
        doSomething(); } } }           }}}



                       Need not be this obvious !!!!
                                                                23
5.1 Deadlock
public void transferMoney(Account fromAccn,
  Account toAccn, Amount amnt) {
    synchronized (fromAccn) {
       synchronized (toAccn) {
         if (fromAccn.hasSufficientBalance(amnt) {
                  fromAccn.debit(amnt);
                  toAccn.credit(amnt);}}}
Thread A : transferMoney(accountOne, accountTwo, amount);
Thread B : transferMoney(accountTwo, accountOne, amount);
                                                            24
5.1 Deadlock
How to avoid Deadlock
     Avoid acquiring more than one lock at a time
     Allow a thread to voluntarily give up its resources if a
      second level or successive lock acquisition fails, this is
      called Two Phase Locking
     Never call a synchronized method of another class from a
      synchronized method
     Follow a fixed order while acquiring and releasing locks
     Induce Lock ordering, if required
       
           Object.identityHashCode() method
                                                                   25
5.1 Deadlock
Banking example revisited
  public void transferMoney(Account fromAccn,
  Account toAccn,Amount amnt) {
  Account firstLck, secondLck;
  if (fromAccn.accountNumber() <
  toAccn.accountNumber()) {
     firstLck = fromAccn;
     secondLck = toAccn;
  } else {
     firstLck = toAccn;
     secondLck = fromAccn; }
                                                26
5.1 Deadlock
synchronized (firstLck) {
 synchronized (secondLck) {
        if (fromAccn.hasSufficientBalance(amnt){
         fromAccn.debit(amnt);
         toAccn.credit(amnt);    }
} } }




                                                   27
6. Synchronization Optimization
JVM optimizes synchronization [under the
 hood]:
     Lock Elision
     Biased Locking
     Lock Coarsening
     Thread Spinning vs Thread Suspending


                                             28
6.1 Synchronization Optimization

    Lock Elision :: Locks on local scope
    references can be elided
    public String concatBuffer(String s1,String s2,String s3) {
        StringBuffer sb = new StringBuffer();
        sb.append(s1);
        sb.append(s2);
        sb.append(s3);
        return sb.toString(); }
                                                                  29
6.2 Synchronization Optimization
• Biased Locking :: Most locks are never
 accessed by more than one thread during
 their life time
  
      Release the lease only if another thread
      attempts to acquire the same lock
  
      Default in Java 6 Hotspot/JIT



                                                 30
6.3 Synchronization Optimization

    Lock coarsening :: Adjacent synchronized
    blocks may be merged into one synchronized
    block
    public static String concatToBuffer(StringBuffer sb, String s1,
     String s2, String s3) {
         sb.append(s1);
         sb.append(s2);
         sb.append(s3);
         return sb.toString();}
                                                                      31
6.4 Synchronization Optimization

    Thread Suspending vs Thread Spinning

    In 1.4.2, spin for (default value of) 10 iterations
    before being suspended

    Adaptive Spinning
       Introduced in 1.6
       Spin duration based on the previous spin attempts
        and state of the lock owner

                                                        32
7.1 Latches

    Latches allows one or more threads to wait
    until a set of operations being performed in
    other threads completes.

    When N concurrent sub-tasks occur, the
    coordinating thread will wait until each sub-
    task is executed



                                                    33
7.1 Latches
class TestLatch {
final CountDownLatch latch = new CountDownLatch(3);
class myThread extends Thread {
    public void run() { task.run();
        latch.countDown()}}
public static void main (String[] args){
    TestLatch tst = new TestLatch();
    tst.new myThread().start();
    tst.new myThread().start();
    tst.new myThread().start();
    latch.await();//Thread execution completed}}
                                                      34
7.2 Barriers

    Barrier allows a set of threads to wait for each
    other at a common barrier point.

    When Barrier point occurs all the threads
    waiting on it are released and run() method of
    Barrier object is called.
class TestBarrier {
    final static int no_of_engines = 4;
    Runnable task = new Runnable() {
            public void run() { flyplane() }}
                                                   35
7.2 Barriers
final CyclicBarrier barrier = new CyclicBarrier(no_of_engines,task);
   class Worker implements Runnable {
       public void run() {
           startEngine();
           barrier.await();} }
public static void main(String[] args) {
        TestBarrier flight = new TestBarrier ();
       for (int i = 0; i < no_of_engines; ++i)
           new Thread(flight.new Worker(i)).start();     }}

                                                                       36
References

    https://www6.software.ibm.com/developerwor
    ks/education/j-concur/index.html

    http://www.infoq.com/articles/java-threading-
    optimizations-p1

    http://www.infoq.com/presentations/goetz-
    concurrency-past-present


                                                    37
Q&A




      38
Thank You !!!!




                 39

More Related Content

PDF
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
PPTX
Java- Concurrent programming - Synchronization (part 2)
PDF
Java Concurrency Quick Guide
PDF
Tech Talks_04.07.15_Session 1_Jeni Markishka & Martin Hristov_Concurrent Prog...
PDF
Concurrent programming without synchronization
PDF
Project02 wit
PPT
Thread safety and code syncronization
PPT
Clh spinlock 实现演示
Monitors and Blocking Synchronization : The Art of Multiprocessor Programming...
Java- Concurrent programming - Synchronization (part 2)
Java Concurrency Quick Guide
Tech Talks_04.07.15_Session 1_Jeni Markishka & Martin Hristov_Concurrent Prog...
Concurrent programming without synchronization
Project02 wit
Thread safety and code syncronization
Clh spinlock 实现演示

Viewers also liked (20)

PDF
Perl 6 for Concurrency and Parallel Computing
PDF
Save JVM by Yourself: Real War Experiences of OOM
PPT
Lock Interface in Java
PPT
Recipe 黃佳伶 葉愛慧
PDF
App开发过程的演变之路
PDF
PDF
Nginx+tomcat https 配置
ODP
Building a lock profiler on the JVM
PDF
大型网站架构演变
PDF
如何更好地设计测试用例-BQConf
PDF
自己的JVM自己救: 解救 OOM 實務經驗談 (JCConf 2015)
PDF
Java多线程技术
PDF
浅谈项目管理(诸葛B2B电商研发部版改)
PPTX
Thrift+scribe实现分布式日志收集,并与log4j集成
PDF
Performance Tuning - Understanding Garbage Collection
PDF
[Java concurrency]02.basic thread synchronization
PPTX
Effective java - concurrency
PDF
JVM及其调优
PPTX
Java concurrency - Thread pools
PDF
淺談 Java GC 原理、調教和 新發展
Perl 6 for Concurrency and Parallel Computing
Save JVM by Yourself: Real War Experiences of OOM
Lock Interface in Java
Recipe 黃佳伶 葉愛慧
App开发过程的演变之路
Nginx+tomcat https 配置
Building a lock profiler on the JVM
大型网站架构演变
如何更好地设计测试用例-BQConf
自己的JVM自己救: 解救 OOM 實務經驗談 (JCConf 2015)
Java多线程技术
浅谈项目管理(诸葛B2B电商研发部版改)
Thrift+scribe实现分布式日志收集,并与log4j集成
Performance Tuning - Understanding Garbage Collection
[Java concurrency]02.basic thread synchronization
Effective java - concurrency
JVM及其调优
Java concurrency - Thread pools
淺談 Java GC 原理、調教和 新發展
Ad

Similar to Concurrency: Best Practices (20)

PPTX
Concurrency
PPTX
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
PPTX
Java concurrency
PPTX
L12 Concurrent Programming
PDF
Concurrency in Java
PDF
Concurrent Programming in Java
PPTX
Concurrency
PDF
Dzone core java concurrency -_
PPTX
Thread syncronization
PPT
Java concurrency
PDF
Concurrency
PPT
Dead locks9cm604.39
PDF
Java Concurrency Gotchas
ODP
Java Concurrency
PDF
Javaoneconcurrencygotchas 090610192215 Phpapp02
PPT
Transactions
DOCX
Java 5 concurrency
DOC
Concurrency Learning From Jdk Source
PDF
Java Concurrency Gotchas
ODP
Java Concurrency, Memory Model, and Trends
Concurrency
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Java concurrency
L12 Concurrent Programming
Concurrency in Java
Concurrent Programming in Java
Concurrency
Dzone core java concurrency -_
Thread syncronization
Java concurrency
Concurrency
Dead locks9cm604.39
Java Concurrency Gotchas
Java Concurrency
Javaoneconcurrencygotchas 090610192215 Phpapp02
Transactions
Java 5 concurrency
Concurrency Learning From Jdk Source
Java Concurrency Gotchas
Java Concurrency, Memory Model, and Trends
Ad

More from IndicThreads (20)

PPTX
Http2 is here! And why the web needs it
ODP
Understanding Bitcoin (Blockchain) and its Potential for Disruptive Applications
PPT
Go Programming Language - Learning The Go Lang way
PPT
Building Resilient Microservices
PPT
App using golang indicthreads
PDF
Building on quicksand microservices indicthreads
PDF
How to Think in RxJava Before Reacting
PPT
Iot secure connected devices indicthreads
PDF
Real world IoT for enterprises
PPT
IoT testing and quality assurance indicthreads
PPT
Functional Programming Past Present Future
PDF
Harnessing the Power of Java 8 Streams
PDF
Building & scaling a live streaming mobile platform - Gr8 road to fame
PPTX
Internet of things architecture perspective - IndicThreads Conference
PDF
Cars and Computers: Building a Java Carputer
PPTX
Scrap Your MapReduce - Apache Spark
PPT
Continuous Integration (CI) and Continuous Delivery (CD) using Jenkins & Docker
PPTX
Speed up your build pipeline for faster feedback
PPT
Unraveling OpenStack Clouds
PPTX
Digital Transformation of the Enterprise. What IT leaders need to know!
Http2 is here! And why the web needs it
Understanding Bitcoin (Blockchain) and its Potential for Disruptive Applications
Go Programming Language - Learning The Go Lang way
Building Resilient Microservices
App using golang indicthreads
Building on quicksand microservices indicthreads
How to Think in RxJava Before Reacting
Iot secure connected devices indicthreads
Real world IoT for enterprises
IoT testing and quality assurance indicthreads
Functional Programming Past Present Future
Harnessing the Power of Java 8 Streams
Building & scaling a live streaming mobile platform - Gr8 road to fame
Internet of things architecture perspective - IndicThreads Conference
Cars and Computers: Building a Java Carputer
Scrap Your MapReduce - Apache Spark
Continuous Integration (CI) and Continuous Delivery (CD) using Jenkins & Docker
Speed up your build pipeline for faster feedback
Unraveling OpenStack Clouds
Digital Transformation of the Enterprise. What IT leaders need to know!

Recently uploaded (20)

PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Cloud computing and distributed systems.
PDF
cuic standard and advanced reporting.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Network Security Unit 5.pdf for BCA BBA.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
The AUB Centre for AI in Media Proposal.docx
Machine learning based COVID-19 study performance prediction
Diabetes mellitus diagnosis method based random forest with bat algorithm
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
A comparative analysis of optical character recognition models for extracting...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Unlocking AI with Model Context Protocol (MCP)
Cloud computing and distributed systems.
cuic standard and advanced reporting.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Reach Out and Touch Someone: Haptics and Empathic Computing
Agricultural_Statistics_at_a_Glance_2022_0.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf

Concurrency: Best Practices

  • 1. Concurrency: Best Practices Pramod B Nagaraja IBM India Pvt LTD 1
  • 2. Agenda  Why Concurrency?  Concurrency Panoramas  Synchronization  When and What to synchronize  Guidelines for designing Concurrent Applications  Excessive Synchronization  Synchronization Optimization  Latches & Barriers 2
  • 3. 1.0 Why Concurrency Story so far….  Most of the applications ran on system with few processors  Relied on faster hardware, rather than Software Concurrency/Parallelism, for better performance 3
  • 4. 1.0 Why Concurrency …Current scenario  Moore's Law predicts that the number of transistors on a chip doubles every two years  Faster Dual core machines demand Concurrency  Multithreading is the pulse of Java 4
  • 5. 2.O Concurrency Panoramas • Atomicity – Certain pieces of an application must all be executed as one unit 5
  • 6. 2.O Concurrency Panoramas  Atomicity synchronized int getBalance() { return balance; } synchronized void setBalance(int x) { balance = x;} void deposit(int x) { void withdraw(int x) { int b = getBalance(); int b = getBalance(); setBalance(b + x);} setBalance(b - x);}} 6
  • 7. 2.O Concurrency Panoramas  Visibility − Changes that you make to a value to be visible precisely when you intend them to be 7
  • 8. 2.O Concurrency Panoramas  Visibility class LoopMayNeverEnd { boolean done = false; void work() { void stopWork() { while (!done) { done = true; } } // do work } } 8
  • 9. 3.0 Synchronization Things which matter MOST should never be at mercy of things which matter LEAST 9
  • 10. 3.1 When to Synchronize Pitfalls when synchronizing:  Forgetting to synchronize a resource that should be synchronized  Synchronizing the wrong resource  Synchronizing too often 10
  • 11. 3.2 What to Synchronize  Not just the resource, but also the Transaction involving the resource  Pay attention to Data integrity at multiple levels of granularity 11
  • 12. 3.2 What to Synchronize private int foo; public synchronized int getFoo() { return foo; } public synchronized void setFoo(int f) { foo = f; } setFoo(getFoo() + 1); //not thread safe 12
  • 13. 4. Guidelines for designing Concurrent Applications  4 common concurrency mechanisms generally used in combination with each other to safely access data from multiple threads − Dynamic Exclusion Through Implicit Locks − Structural Exclusion Through Confinement − Immutability − Cooperation 13
  • 14. 4.1 Dynamic Exclusion Through Implicit Locks Lock associated with an object is acquired when a method is declared synchronized. public synchronized void setName(String name);  Limitation − When synchronization is used inappropriately or excessively it causes poor performance and deadlock. 14
  • 15. 4.1 Dynamic Exclusion Through Implicit Locks  Best Practice Tip  Do not perform CPU intensive and I/O operations inside synchronized method.  When possible synchronize on block of code instead of synchronizing entire method. Ex: synchronize(lock) { //critical section guarded by lock} 15
  • 16. 4.2 Structural Exclusion Through Confinement  Thread confinement : Access object from a single thread using thread-per-session approach  Instance confinement : Use encapsulation techniques to restrict access to object state from multiple threads  Method confinement : Do not allow an object to escape from method by referencing it through local variables 16
  • 17. 4.2 Structural Exclusion Through Confinement  Limitations : − Confinement cannot be relied on unless a design is leak proof  Best Practice Tip :  Combine confinement with appropriate locking discipline  Use ThreadLocal variables to maintain Thread Confinement 17
  • 18. 4.3 Immutability  Immutable objects do not need additional synchronization afe { Public final class ImmutableClass ea d-s private final intThr = 100; field1 ly ent String field2=“FIN”; private final In her ……….. } 18
  • 19. 4.3 Immutability  Limitation − In a software application only some classes can be immutable.  Best Practice Tip − Even when class is not fully immutable, declare its non changeable fields as final to limit its mutability. 19
  • 20. 4.4 Cooperation What is purpose of wait-and-notify mechanism? When one thread needs a certain condition to exist and another thread can create that condition then we use wait and notify methods. Thread 1 Thread 2 CONDITION MET STOP!! CONDITION NOT MET Thread 1 can proceed cannot progress Thread 1 Thread 2 - “I will meet your condition.” 20
  • 21. 4.4 Cooperation Limitation − When there are multiple waiting threads, you cannot be certain which thread is woken up. − Due to race conditions there could be missed or early notifications. Best Practice Tip  Safe to wake up all the threads using notifyAll() method instead of waking an arbitrary thread using notify()  Sometimes condition that caused thread to wait is not achieved when wait() returns, therefore put wait() call in a while loop.  Wait() and notify() must always be called inside synchronized code. 21
  • 22. 5. Excessive Synchronization  Poor performance  Deadlock  Can occur when multiple threads each acquire multiple locks in different orders 22
  • 23. 5.1 Deadlock public static Object cacheLock = new Object(); public static Object tableLock = new Object(); ... public void oneMethod() { public void anotherMethod() { synchronized (cacheLock) { synchronized (tableLock) { synchronized (cacheLock) { synchronized (tableLock) { doSomethingElse(); doSomething(); } } } }}} Need not be this obvious !!!! 23
  • 24. 5.1 Deadlock public void transferMoney(Account fromAccn, Account toAccn, Amount amnt) { synchronized (fromAccn) { synchronized (toAccn) { if (fromAccn.hasSufficientBalance(amnt) { fromAccn.debit(amnt); toAccn.credit(amnt);}}} Thread A : transferMoney(accountOne, accountTwo, amount); Thread B : transferMoney(accountTwo, accountOne, amount); 24
  • 25. 5.1 Deadlock How to avoid Deadlock  Avoid acquiring more than one lock at a time  Allow a thread to voluntarily give up its resources if a second level or successive lock acquisition fails, this is called Two Phase Locking  Never call a synchronized method of another class from a synchronized method  Follow a fixed order while acquiring and releasing locks  Induce Lock ordering, if required  Object.identityHashCode() method 25
  • 26. 5.1 Deadlock Banking example revisited public void transferMoney(Account fromAccn, Account toAccn,Amount amnt) { Account firstLck, secondLck; if (fromAccn.accountNumber() < toAccn.accountNumber()) { firstLck = fromAccn; secondLck = toAccn; } else { firstLck = toAccn; secondLck = fromAccn; } 26
  • 27. 5.1 Deadlock synchronized (firstLck) { synchronized (secondLck) { if (fromAccn.hasSufficientBalance(amnt){ fromAccn.debit(amnt); toAccn.credit(amnt); } } } } 27
  • 28. 6. Synchronization Optimization JVM optimizes synchronization [under the hood]:  Lock Elision  Biased Locking  Lock Coarsening  Thread Spinning vs Thread Suspending 28
  • 29. 6.1 Synchronization Optimization  Lock Elision :: Locks on local scope references can be elided public String concatBuffer(String s1,String s2,String s3) { StringBuffer sb = new StringBuffer(); sb.append(s1); sb.append(s2); sb.append(s3); return sb.toString(); } 29
  • 30. 6.2 Synchronization Optimization • Biased Locking :: Most locks are never accessed by more than one thread during their life time  Release the lease only if another thread attempts to acquire the same lock  Default in Java 6 Hotspot/JIT 30
  • 31. 6.3 Synchronization Optimization  Lock coarsening :: Adjacent synchronized blocks may be merged into one synchronized block public static String concatToBuffer(StringBuffer sb, String s1, String s2, String s3) { sb.append(s1); sb.append(s2); sb.append(s3); return sb.toString();} 31
  • 32. 6.4 Synchronization Optimization  Thread Suspending vs Thread Spinning  In 1.4.2, spin for (default value of) 10 iterations before being suspended  Adaptive Spinning  Introduced in 1.6  Spin duration based on the previous spin attempts and state of the lock owner 32
  • 33. 7.1 Latches  Latches allows one or more threads to wait until a set of operations being performed in other threads completes.  When N concurrent sub-tasks occur, the coordinating thread will wait until each sub- task is executed 33
  • 34. 7.1 Latches class TestLatch { final CountDownLatch latch = new CountDownLatch(3); class myThread extends Thread { public void run() { task.run(); latch.countDown()}} public static void main (String[] args){ TestLatch tst = new TestLatch(); tst.new myThread().start(); tst.new myThread().start(); tst.new myThread().start(); latch.await();//Thread execution completed}} 34
  • 35. 7.2 Barriers  Barrier allows a set of threads to wait for each other at a common barrier point.  When Barrier point occurs all the threads waiting on it are released and run() method of Barrier object is called. class TestBarrier { final static int no_of_engines = 4; Runnable task = new Runnable() { public void run() { flyplane() }} 35
  • 36. 7.2 Barriers final CyclicBarrier barrier = new CyclicBarrier(no_of_engines,task); class Worker implements Runnable { public void run() { startEngine(); barrier.await();} } public static void main(String[] args) { TestBarrier flight = new TestBarrier (); for (int i = 0; i < no_of_engines; ++i) new Thread(flight.new Worker(i)).start(); }} 36
  • 37. References  https://www6.software.ibm.com/developerwor ks/education/j-concur/index.html  http://www.infoq.com/articles/java-threading- optimizations-p1  http://www.infoq.com/presentations/goetz- concurrency-past-present 37
  • 38. Q&A 38