SlideShare a Scribd company logo
Java concurrency - beginning




                     Stetsenko Maksym
                     stetsenko89@gmail.com
Lead-in
Objective of concurrency - optimal utilization of system resources


Advantages - increases bandwidth of applications



Drawbacks - increases the complexity of the design, testing and maintenance




Used in:



              Parallel processing algorithms

              Network applications, asynchronous I / O access

              GUI
How to create thread in java ?
  1. Extends Thread:                                2. Implement Runnable
  Advantages:
                                                    Advantages:
  - full access to functionality of the thread
                                                    - Inheritance from another object is still available

  Disadvantages:
  - can not be inherited from another class , but
  delegation mechanism can be used alternatively




void run() - must be overridden in both cases


void start() - causes this thread to begin execution; the JVM calls the run method of this thread.
 Can be called only once for each thread.
Race condition
Race Condition is a type of flaw in an electronic or software system where the output is depend on the sequence or timing of
    other events.



Preconditions:
Two or more threads are sharing the same data.
At least one thread performs modification.
                                                                            // Thread 2:
Example:                             int x;
                                                                            while (!stop)
                                     // Thread 1:
                                                                            {
                                     while (!stop)
                                                                                 if (x%2 == 0)
                                     {
                                                                                           System.out.println("x="
                                         x++;
                                                                            + x);
                                         …
                                                                                 …
                                     }
                                                                            }


      Let's assume x=0. Suppose execution of program is performed in such way:

      1. if - checks x for parity in thread 2.
      2. Operator «x++» increases x in thread 1.
      3. System.out.println in thread 2 displays «x=1». How come ? (We have already checked x for parity ).



      Synchronization is needed, isn't it ? :)
Monitor
Monitor – it's an object, which is used for mutually exclusive lock.
Any object is a monitor in Java. This ability is inherited from Object class.



When one thread comes into synchronized block, it acquires a lock. All other threads have to wait until lock will be released.
Lock can be released in such cases:
- Synchronized block was successfully completed.
- Exception has been thrown.
Control under the monitor can be passed into other synchronized block:
- in all previous cases
- and when method wait has been called. (dead-clock state dangerous)



note: If static block or method is synchronized, then lock of the class is acquired. Access to static fields and methods is controlled by lock of the class that is
      different from lock of each instance.
                                                           Examples of lock acquiring:
                           Acquiring the lock of this:                                                  Acquiring the lock of another object
                                                                                                        (lock1):
public synchronized void addName() {                       public void addName() {                      public class MуClass {
...                                                            synchronized(this) {                       private long c1 = 0;
}                                                                 ...                                     private Object lock1 = new Object();
                                                                       }                                  public void inc1() {
                                                                                                                    synchronized(lock1) {
                                                           nameList.add("Bob");                                     c1++;
                                                           }                                                        }
                                                                                                          }
Basic tools: wait, notify, notifyAll
public final void wait() / wait(long timeout) / notify () / notifyAll () - defined in Object class let us manage the thread state:
wait() – moves current thread to waiting state, releases the lock.
wait(timeout) - moves current thread to waiting state for specified time. Exit will be performed either:
•      time elapsed;
•      notification is received.



notify() / notifyAll() – notifies waiting thread / all waiting threads on this object's monitor that they can try to acquire the lock.
                            Notification will be received by threads that have previously called wait. It's up to OS which thread will be
      resumed.


note: wait() и notify(), notifyAll() must be called for acquired lock, otherwise java.lang.IllegalMonitorStateException will be thrown.
                                               Applying autoboxing and enums as locks are slippery:

       public class test {                                                        RESULT:
           static Integer foo = new Integer(1);                                   Exception in thread "main" java.lang.IllegalMonitorStateException
           public static void main(String[] args) {                                     at java.lang.Object.notifyAll(Native Method)
               synchronized(foo) {                                                      at test.main(test.java:6)
                 foo++;                                                           ...
                 foo.notifyAll(); }
           }
       }
Basic tools: sleep, join
Defined in Thread class:

public static void sleep(long millis) - Causes the currently executing thread to sleep for the specified number of milliseconds.
                                                                               Method doesn't release the lock.
public final void join() throws InterruptedExeption – waits for this thread to die. A lock doesn't have to be pre-acquired.


   public class JoinTest {
      public static void main(String[] args) {
        Thread thread = new Thread() {
           @Override                                                                       // throw new NullPointerException();
           public void run() {
               System.out.println(                                                         main will be join to Thread-0
                  Thread.currentThread().getName() + " is running");                       Thread-0 is running
               try {                                                                       Thread-0 is TERMINATED
                  TimeUnit.SECONDS.sleep(4);                                               main thread after join
               } catch (InterruptedException e) {
                  System.out.println(e + " in " + Thread.currentThread().getName());
               }
   //        throw new NullPointerException();
           }                                                                               throw new NullPointerException();
        };

        thread.start();                                                                    main will be join to Thread-0
        try {                                                                              Thread-0 is running
           System.out.println("main will be join to " + thread.getName());                 Exception in thread "Thread-0"
           thread.join();                                                                  java.lang.NullPointerException
                                                                                           Thread-0 is TERMINATED
        } catch (InterruptedException e) {
                                                                                                           at
           System.out.println(e + " in main");                                             JoinTest$1.run(JoinTest.java:24)
        }                                                                                  main thread after join
        System.out.println(thread.getName() + " is " + thread.getState());
        System.out.println("main thread after join");
Interruption
Interruption - is a signal for the thread that it should stop an execution.

Each thread has to handle his own interruption.

public void interrupt() - interrupts this thread.

scenarios:
 - clears intrerrupt status, throws InterruptedException;
 - sets intrerrupt status;
 - does nothing.



                                                    How to find out interrupt status?

       static boolean interrupted()                                   boolean isInterrupted()


       clears interrupt status                                        doesn't clear interrupt status



Interruption processing example:
if (Thread.interrupted()) {
// do some work if needed
     throw new InterruptedException();
}
Volatile
There are several ways that can enhance performance:


•    storing data in registers


•    changing the order of operations
Example:
                                    private boolean done = false;       private volatile boolean done = false;

public void run( ) {                Begin method run                    Begin method run
                                    Load register r1 with memory        Label L1:
     while (!done) {                location 0Xff12345                  Test if memory location 0xff12345 ==
     foo( );
                                    Label L1:                           1
                                    Test if register r1 == 1            If true branch to L2
     }                              If true branch to L2                Call method foo
                                    Call method foo                     Branch to L1
}
                                    Branch to L1                        Label L2:
                                    Label L2:                           End method
                                    End method run
public void setDone( ) {
     done = true;
}

                                    done is read only from register -   done is read from main memory - there
                                    operations always will be looped    is a chance to escape from loop




Keyword volatile guaranties that value is never stored to registers.

Synchronization has the similar effect because of registers' invalidation
Changing operations' order
We can not rely on operations' order to avoid costs of synchronization:



Example:
public int currentScore, totalScore, finalScore


public void resetScore(boolean done) {                  finalScore = totalScore;                   currentScore = 0;
                                                        currentScore = 0;                          finalScore = totalScore;

     totalScore += currentScore;
     if (done) {
                                                     Thread1: Update total score         Thread1: Update total score
     finalScore = totalScore;
                                                     Thread2: See if currentScore == 0   Thread1: Set currentScore == 0
     currentScore = 0;
                                                     Thread2: Return -1                  Thread2: See if currentScore == 0
     }
                                                     Thread1: Update finalScore          Thread2: Return finalScore
}
                                                     Thread1: Set currentScore == 0      Thread1: Update finalScore

public int getFinalScore( ) {
                                                             logic is correct                     logic is corrupted
     if (currentScore == 0)
     return finalScore;
    return -1;
}

                                    Synchronization protects operations from
                                                    mixing:
JVM cannot move operation outside the synchronization block.
But operation that is located before or after synchronization block can be moved into synchronization block.
Lock starvation
Lock starvation occurs when a particular thread attempts to acquire a lock and never succeeds because another thread is already
     holding the lock.



Reasons:


The thread knows nothing about other contestants.
The thread with higher priority is served first.
A generous amount of threads.



Synchronized blocks within loops often have this problem:



while(true) {
         synchronized (this) {
               // execute some code
     }
}
ReentrantLock
Solves lock starvation problem
Construct a ReentrantLock object where the fairness flag is set to true.
Threads are served very close to a first-come, first-served basis regardless of the thread's priority.



Drawbacks:
The priority can be set because the developer wanted to have the scheduler behave in a certain manner. ReentrantLock may
    change the desired scheduling behavior.


                                                                                             fairness false        fairness true
Example:
                                                                                            Thread-0 before sb   Thread-1 before sb
                                                                                            Thread-9 before sb   Thread-2 before sb
final ReentrantLock _lock = new ReentrantLock(true);                                        Thread-3 before sb   Thread-5 before sb
                                                                                            Thread-7 before sb   Thread-6 before sb
@Override                                                                                   Thread-4 before sb   Thread-6 in sb
public void run() {                                                                         Thread-6 before sb   Thread-1 in sb
     System.out.println(Thread.currentThread().getName() + " before sb");
     _lock.lock(); // will wait until this thread gets the lock
                                                                                            Thread-5 before sb   Thread-2 in sb
     try                                                                                    Thread-2 before sb   Thread-5 in sb
     {                                                                                      Thread-1 before sb   Thread-3 before sb
         System.out.println(Thread.currentThread().getName() + " in sb");                   Thread-2 in sb       Thread-3 in sb
     finally                                                                                Thread-5 in sb       Thread-7 before sb
     {                                                                                      Thread-6 in sb       Thread-7 in sb
         //releasing the lock so that other threads can get notifies
          _lock.unlock();
                                                                                            Thread-4 in sb       Thread-9 before sb
     }                                                                                      Thread-7 in sb       Thread-9 in sb
}                                                                                           Thread-3 in sb       Thread-0 before sb
                                                                                            Thread-9 in sb       Thread-0 in sb
                                                                                            Thread-0 in sb       Thread-4 before sb
                                                                                            Thread-1 in sb       Thread-4 in sb
Reader/Writer Locks
ReentrantReadWriteLock class.


Generally, reader/writer locks are used when there are many more readers than writers;
The reader/writer lock is needed—to share data access during the long periods of time when only reading is needed.
ReadLock - can be shared between readers
WriteLock - exclusive



work principle:



preconditions: there are lots of writers and readers;



behaviour (fairness = false) :


             Reader has finished task                             Writer will be called;


             Writer has finished task                             Reader or Writer will be called
                                                                   (arbitrary schedule);


behaviour (fairness = true) :
Threads are served as first-come, first-served basis
ReentrantReadWriteLock example
  static class RRWLC {
                                                                                                     Result:
      ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
      private final Lock read = readWriteLock.readLock();                                            Reader :Thread-7 is reading : sb =
                                                                                                     Reader :Thread-5 is reading : sb =
      private final Lock write = readWriteLock.writeLock();                                          Reader :Thread-0 is reading : sb =
                                                                                                     Reader :Thread-2 is reading : sb =
      StringBuilder sb = new StringBuilder();
                                                                                                     Reader :Thread-3 is reading : sb =
                                                                                                     Reader :Thread-5 has finished reading
                                                                                                     Reader :Thread-3 has finished reading
                                                                                                     Reader :Thread-0 has finished reading
      public void read() {                                                                           Reader :Thread-2 has finished reading
                                                                                                     Reader :Thread-7 has finished reading
          read.lock();                                                                               Writer :Thread-8 is writing Now sb = +
          try{ TimeUnit.SECONDS.sleep(2); //sout                                                     Writer :Thread-8 has finished writing
                                                                                                     Writer :Thread-9 is writing Now sb = + +
          } finally {         read.unlock(); //sout             }                                    Writer :Thread-9 has finished writing
                                                                                                     Reader :Thread-1 is reading : sb = + +
      }                                                                                              Reader :Thread-4 is reading : sb = + +
                                                                                                     Reader :Thread-1 has finished reading
                                                                                                     Reader :Thread-4 has finished reading
                                                                                                     Writer :Thread-11 is writing Now sb = + + +
      public void write (){                                                                          Writer :Thread-11 has finished writing
                                                                                                     Reader :Thread-6 is reading : sb = + + +
          write.lock();                                                                              Reader :Thread-6 has finished reading
                                                                                                     Writer :Thread-10 is writing Now sb = + + + +
          try{                                                                                       Writer :Thread-10 has finished writing
System.out.println("Now sb = " + sb.append(" + "));//sout
          } finally{          write.unlock(); //sout }
      }
  }


             static class Reader extends Thread {                   static class Writer extends Thread {
             private RRWLC rrwlc;                                   private RRWLC rrwlc;
              @Override                                              @Override
                   public void run() { ... rrwlc.read(); ...}             public void run() { ... rrwlc.write(); ...}
             }                                                      }
Thread Local Variables
Thread local variable:
- is private to particular thread;
- cannot be used to share state between threads;
- access to the variable need never be synchronized;


If TLV has never been set before the initialValue() is called to return the value.



     Qualities                         Thread Local                       Local Object

     state independency                +                                  +

     Common monitor exists             +                                  -

     Lazy initialization               +                                  -

     disadvantages:                    Perfomance loses on
                                       chosing/getting object
Thread Local example
public class TestTreadLocal
{
    public static void main(String[] args) {
        final ThreadLocal<MyObject> perThreadCounter = new ThreadLocal<MyObject>();



        Thread t1 = new Thread() {                                             Thread t2 = new Thread() {
             public void run() {                                                           public void run() {
                 perThreadCounter.set(new MyObject(2));                                        perThreadCounter.set(new MyObject(3));
                 info(perThreadCounter);                                                       info(perThreadCounter);
                 perThreadCounter.get().setX(8);                                               perThreadCounter.get().setX(7);
                 timeout(3);                                                                   timeout(3);
                 info(perThreadCounter);                                                       info(perThreadCounter);
             }                                                                             }
        };                                                                            };
        System.out.println("In main : " + perThreadCounter.get());
        t1.start();
        timeout(1);
        t2.start();                 Result:
    }
                                    In main : null
                                    Thread-0 : java.lang.ThreadLocal@4ce86da0 : concurrency.TestTreadLocal2$MyObject@2f754ad2 : 2
                                    Thread-1 : java.lang.ThreadLocal@4ce86da0 : concurrency.TestTreadLocal2$MyObject@95c7850 : 3
                                    Thread-1 : java.lang.ThreadLocal@4ce86da0 : concurrency.TestTreadLocal2$MyObject@95c7850 : 7
                                    Thread-0 : java.lang.ThreadLocal@4ce86da0 : concurrency.TestTreadLocal2$MyObject@2f754ad2 : 8
Atomic operations and classes
Allows operation that contains multiple instructions can be treated as atomic (for example ++) ;




Classes: AtomicInteger, AtomicLong, AtomicBoolean, and AtomicReference.



get() and set() - provide volatile functionality.

getAndSet() - sets the new value returns the previous value.
      compareAndSet (exp, new)
                                              compare exp with current value. if they are equal - set new value, return true.
       weakCompareAndSet (exp, new)




The AtomicInteger and AtomicLong additional methods:



incrementAndGet(), getAndIncrement() - pre/post-increment

decrementAndGet(), getAndDecrement() - pre/post-decrement

addAndGet(), getAndAdd() - pre- and post-operators for the delta value.
Atomic field updaters
AtomicIntegerFieldUpdater, AtomicLongFieldUpdater, AtomicReferenceFieldUpdater can help to update volatile variables.


These classes are abstract. Use the static newUpdater() method.


Features:




         •     Is used as "wrappers" around a volatile field;


         •     Don't require adding an extra object for atomic access to your class;


         •     Let refer to the variable "normally" (without get, set);


To avoid using the synchronization via the atomic classes we have to make:



         •    Simple variable substitution;


         •    Changing algorithms;


         •    Retrying operations;
Implementation differences
      synchronized                                  AtomicInteger                                        AtomicIntegerFieldUpdater

public class AtomicCounter {   publiclass AtomicCounter {                               public class AtomicIntegerFieldUpdaterExample {

int value = 0;                 private final AtomicInteger value = new                      public static void main(String[] args) {
                               AtomicInteger(0);                                              Counter c1 = new Counter(10);
public synchronized void                                                                      Counter c2 = new Counter(20);
increment () {                 public int increment () {
value++;                       return value.incrementAndGet();                                   AtomicIntegerFieldUpdater<Counter> iUpdater =
}                              }
                                                                                        AtomicIntegerFieldUpdater.newUpdater(Counter.class,
                               // or via compareAndSet                                  "value");

                               public void increment() {                                        iUpdater.incrementAndGet(c1);
                                      while (true) {                                            iUpdater.incrementAndGet(c2);
                                         int current = value.get();                             System.out.println("c1.value = " + iUpdater.get(c1));
                                         int next = current + 1;                                System.out.println("c2.value = " + iUpdater.get(c2));
                                                                                            }
                                            if (integer.compareAndSet(current, next))
                               {                                                            static class Counter{
                                                return;
                                            }                                                    volatile int value; // private is not allowed
                                        }
                                    }                                                            Counter(int value) { this.value = value; }
                                                                                            }
                                                                                        }
Semaphore
Semaphore is a lock with a counter.
Lets different threads acquire lock one or more times.



Features:


         •    A number of simultaneously executing threads have to be set in the constructor;


         •    The acquire() and release() are similar to the lock() and unlock() methods of the

              Lock;


         •    Supply fairness politics.


         •    There is no concept of nesting;
Semaphore example
static class SemaphoreTester extends Thread {                                                   public class SemaphoreExample {
                                                                                                  public static void main(String[] args) {
    private Semaphore s;
    SemaphoreTester(Semaphore s) { this.s = s;}                                                       Semaphore sem = new Semaphore(3);
    @Override                                                                                         SemaphoreTester st1 = new SemaphoreTester(sem);
    public void run() {                                                                               SemaphoreTester st2 = new SemaphoreTester(sem);
       try {                                                                                          st1.start();
          System.out.println(getName() + " at first : is trying to acquire the lock ");               st2.start();
          s.acquire();                                                                            }
          System.out.println(getName() + " semaphore's first acquire has got");
          System.out.println(getName() + " second time: is trying to acquire the lock " +
TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
          s.acquire();
          System.out.println(getName() + " semaphore's second acquire " +
TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
          TimeUnit.SECONDS.sleep(5);                                                 Result:
          s.release();
          System.out.println(getName() + " semaphore has been released"); Thread-1         at first : is trying to acquire the lock
          s.release();                                                               Thread-1
                                                                                           semaphore's first acquire has got
          System.out.println(getName() + " semaphore has been released"); Thread-1         second time: is trying to acquire the lock 1351581890
       } catch (InterruptedException e) {                                            Thread-1
                                                                                           semaphore's second acquire 1351581890
          e.printStackTrace(); lates.                                                Thread-0
                                                                                           at first : is trying to acquire the lock
       }                                                                             Thread-0
                                                                                           semaphore's first acquire has got
                                                                                  Thread-0 second time: is trying to acquire the lock 1351581890
        }                                                                         Thread-1 semaphore has been released
    }                                                                             Thread-1 semaphore has been released
}                                                                                 Thread-0 semaphore's second acquire 1351581895
                                                                                  Thread-0 semaphore has been released
                                                                                  Thread-0 semaphore has been released
Barrier
The barrier is simply a waiting point where all the threads can sync up.
The last thread releases the barrier by notifying all of the other threads.


             stage 1       waiting           stage 2        waiting           stage 3
                                                                                        ...
        stage 1 waiting                        stage 2          waiting       stage 3   ...

                  stage 1      waiting     stage 2        waiting             stage 3   ...

Barrier against join()
benefits:
we mustn't always create new threads;
logical operations can be placed together;
drawbacks:
     complex reinitialization process;



Barrier can be broken when:
1. Waiting threads can be interrupted;
2. Thread may break through the barrier due to a timeout condition;
3. Exception could be thrown by the barrier action.
CyclicBarrier example
public class CyclicBarrierExample                                         private static class Summer extends Thread {
{                                                                             int row;
    private static int matrix[][] =                                           CyclicBarrier barrier;
    {{ 1 }, { 2, 2 }, { 3, 3, 3 }, { 4, 4, 4, 4 }, { 5, 5, 5, 5, 5 }};
    private static int results[];                                             Summer(CyclicBarrier barrier, int row)
                                                                              { this.barrier = barrier;
                                                                                this.row = row; }
    public static void main(String args[])
    {
                                                                              public void run() {
        final int rows = matrix.length;
                                                                                int columns = matrix[row].length;
        results = new int[rows];
                                                                                int sum = 0;
        Runnable merger = new Runnable(){
                                                                                for (int i = 0; i < columns; i++)
          public void run()
                                                                                              sum += matrix[row][i];
          { int sum = 0;
                                                                                results[row] = sum;
            for (int i = 0; i < rows; i++)
                                                                                System.out.println(Thread.currentThread().getName() + ":
                     sum += results[i];                                   Results for row " + row + " are : " + sum);
            System.out.println("Merger :"+ " Results are: " + sum); }};         try{
                                                                                  barrier.await();            // wait for others
                                                                                  System.out.println(Thread.currentThread().getName() +
        CyclicBarrier barrier = new CyclicBarrier(rows,
         merger);                                                         " do something else " + System.currentTimeMillis());
        for (int i = 0; i < rows; i++)                                          } catch ...
        { new Summer(barrier, i).start();                   }
    }
Result:

      Thread-0: Results for row 0 are : 1
      Thread-1: Results for row 1 are : 4
      Thread-2: Results for row 2 are : 9
      Thread-4: Results for row 4 are : 25
      Thread-3: Results for row 3 are : 16
      Merger : Results are: 55
      Thread-3 do something else 1351699717125
      Thread-0 do something else 1351699717125
      Thread-1 do something else 1351699717125
      Thread-2 do something else 1351699717125
      Thread-4 do something else 1351699717125
The end
References
Books:
Java Threads, Third Edition; Scott Oaks & Henry Wong; ISBN: 978-0-596-00782-9. O'Reilly, 2004.
Concurrent Programming in Java: Design Principles and Patterns (2nd edition) Doug Lea; ISBN 0-201-31009-0.
    AddisonWesley, 1999.



Web recources:
http://www.javamex.com/tutorials/synchronization_concurrency_7_atomic_updaters.shtml
http://www.baptiste-wicht.com/2010/09/java-concurrency-atomic-variables/
http://www.ibm.com/developerworks/java/library/j-jtp11234/
http://programmingexamples.wikidot.com/cyclicbarrier
http://javarevisited.blogspot.com/2012/07/cyclicbarrier-example-java-5-concurrency-tutorial.html
http://tutorials.jenkov.com/java-concurrency/semaphores.html
http://alexbtw.livejournal.com/3355.html

More Related Content

PPTX
Effective java - concurrency
PDF
Java Concurrency Gotchas
ODP
Java Concurrency
PDF
Java Concurrency Idioms
PPT
Inter threadcommunication.38
PPTX
Inter thread communication &amp; runnable interface
PPTX
分散式系統
PPT
final year project center in Coimbatore
Effective java - concurrency
Java Concurrency Gotchas
Java Concurrency
Java Concurrency Idioms
Inter threadcommunication.38
Inter thread communication &amp; runnable interface
分散式系統
final year project center in Coimbatore

What's hot (20)

DOCX
Java 5 concurrency
PPT
04 threads
PDF
Sam wd programs
PPTX
PDF
13multithreaded Programming
ODP
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
PDF
.NET Multithreading and File I/O
PDF
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
PDF
Java Concurrency Gotchas
PDF
Advanced Java Practical File
PDF
Java Programming - 03 java control flow
PDF
Actor Concurrency
TXT
Play image
PPTX
Java concurrency
DOCX
Jist of Java
DOCX
JAVA CONCEPTS AND PRACTICES
PPT
Learning Java 1 – Introduction
PDF
sizeof(Object): how much memory objects take on JVMs and when this may matter
PDF
Important java programs(collection+file)
KEY
Java 5 concurrency
04 threads
Sam wd programs
13multithreaded Programming
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
.NET Multithreading and File I/O
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
Java Concurrency Gotchas
Advanced Java Practical File
Java Programming - 03 java control flow
Actor Concurrency
Play image
Java concurrency
Jist of Java
JAVA CONCEPTS AND PRACTICES
Learning Java 1 – Introduction
sizeof(Object): how much memory objects take on JVMs and when this may matter
Important java programs(collection+file)
Ad

Similar to Java concurrency begining (20)

PPS
Java session13
KEY
ぐだ生 Java入門第ニ回(synchronized and lock)
KEY
ぐだ生 Java入門第ニ回(synchronized and lock)
PDF
Java synchronizers
PPT
Threadlifecycle.36
PPTX
Concurrency in Programming Languages
PDF
Java Runtime: повседневные обязанности JVM
PDF
Java Week9(A) Notepad
PPT
Java concurrency
PPTX
Java class 6
PPTX
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
PDF
Concurrencyproblem
PPT
Thread 1
PDF
Concurrency
PPT
ThreadProperties
PDF
Concurrency
PDF
Java Thread Synchronization
KEY
Java and the machine - Martijn Verburg and Kirk Pepperdine
PPTX
Java practice programs for beginners
PDF
Java Concurrency by Example
Java session13
ぐだ生 Java入門第ニ回(synchronized and lock)
ぐだ生 Java入門第ニ回(synchronized and lock)
Java synchronizers
Threadlifecycle.36
Concurrency in Programming Languages
Java Runtime: повседневные обязанности JVM
Java Week9(A) Notepad
Java concurrency
Java class 6
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Concurrencyproblem
Thread 1
Concurrency
ThreadProperties
Concurrency
Java Thread Synchronization
Java and the machine - Martijn Verburg and Kirk Pepperdine
Java practice programs for beginners
Java Concurrency by Example
Ad

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
RMMM.pdf make it easy to upload and study
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Institutional Correction lecture only . . .
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
01-Introduction-to-Information-Management.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
VCE English Exam - Section C Student Revision Booklet
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Module 4: Burden of Disease Tutorial Slides S2 2025
RMMM.pdf make it easy to upload and study
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Anesthesia in Laparoscopic Surgery in India
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Microbial disease of the cardiovascular and lymphatic systems
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Supply Chain Operations Speaking Notes -ICLT Program
102 student loan defaulters named and shamed – Is someone you know on the list?
Institutional Correction lecture only . . .
PPH.pptx obstetrics and gynecology in nursing
O5-L3 Freight Transport Ops (International) V1.pdf
Renaissance Architecture: A Journey from Faith to Humanism
01-Introduction-to-Information-Management.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Week 4 Term 3 Study Techniques revisited.pptx

Java concurrency begining

  • 1. Java concurrency - beginning Stetsenko Maksym stetsenko89@gmail.com
  • 2. Lead-in Objective of concurrency - optimal utilization of system resources Advantages - increases bandwidth of applications Drawbacks - increases the complexity of the design, testing and maintenance Used in:  Parallel processing algorithms  Network applications, asynchronous I / O access  GUI
  • 3. How to create thread in java ? 1. Extends Thread: 2. Implement Runnable Advantages: Advantages: - full access to functionality of the thread - Inheritance from another object is still available Disadvantages: - can not be inherited from another class , but delegation mechanism can be used alternatively void run() - must be overridden in both cases void start() - causes this thread to begin execution; the JVM calls the run method of this thread. Can be called only once for each thread.
  • 4. Race condition Race Condition is a type of flaw in an electronic or software system where the output is depend on the sequence or timing of other events. Preconditions: Two or more threads are sharing the same data. At least one thread performs modification. // Thread 2: Example: int x; while (!stop) // Thread 1: { while (!stop) if (x%2 == 0) { System.out.println("x=" x++; + x); … … } } Let's assume x=0. Suppose execution of program is performed in such way: 1. if - checks x for parity in thread 2. 2. Operator «x++» increases x in thread 1. 3. System.out.println in thread 2 displays «x=1». How come ? (We have already checked x for parity ). Synchronization is needed, isn't it ? :)
  • 5. Monitor Monitor – it's an object, which is used for mutually exclusive lock. Any object is a monitor in Java. This ability is inherited from Object class. When one thread comes into synchronized block, it acquires a lock. All other threads have to wait until lock will be released. Lock can be released in such cases: - Synchronized block was successfully completed. - Exception has been thrown. Control under the monitor can be passed into other synchronized block: - in all previous cases - and when method wait has been called. (dead-clock state dangerous) note: If static block or method is synchronized, then lock of the class is acquired. Access to static fields and methods is controlled by lock of the class that is different from lock of each instance. Examples of lock acquiring: Acquiring the lock of this: Acquiring the lock of another object (lock1): public synchronized void addName() { public void addName() { public class MуClass { ... synchronized(this) { private long c1 = 0; } ... private Object lock1 = new Object(); } public void inc1() { synchronized(lock1) { nameList.add("Bob"); c1++; } } }
  • 6. Basic tools: wait, notify, notifyAll public final void wait() / wait(long timeout) / notify () / notifyAll () - defined in Object class let us manage the thread state: wait() – moves current thread to waiting state, releases the lock. wait(timeout) - moves current thread to waiting state for specified time. Exit will be performed either: • time elapsed; • notification is received. notify() / notifyAll() – notifies waiting thread / all waiting threads on this object's monitor that they can try to acquire the lock. Notification will be received by threads that have previously called wait. It's up to OS which thread will be resumed. note: wait() и notify(), notifyAll() must be called for acquired lock, otherwise java.lang.IllegalMonitorStateException will be thrown. Applying autoboxing and enums as locks are slippery: public class test { RESULT: static Integer foo = new Integer(1); Exception in thread "main" java.lang.IllegalMonitorStateException public static void main(String[] args) { at java.lang.Object.notifyAll(Native Method) synchronized(foo) { at test.main(test.java:6) foo++; ... foo.notifyAll(); } } }
  • 7. Basic tools: sleep, join Defined in Thread class: public static void sleep(long millis) - Causes the currently executing thread to sleep for the specified number of milliseconds. Method doesn't release the lock. public final void join() throws InterruptedExeption – waits for this thread to die. A lock doesn't have to be pre-acquired. public class JoinTest { public static void main(String[] args) { Thread thread = new Thread() { @Override // throw new NullPointerException(); public void run() { System.out.println( main will be join to Thread-0 Thread.currentThread().getName() + " is running"); Thread-0 is running try { Thread-0 is TERMINATED TimeUnit.SECONDS.sleep(4); main thread after join } catch (InterruptedException e) { System.out.println(e + " in " + Thread.currentThread().getName()); } // throw new NullPointerException(); } throw new NullPointerException(); }; thread.start(); main will be join to Thread-0 try { Thread-0 is running System.out.println("main will be join to " + thread.getName()); Exception in thread "Thread-0" thread.join(); java.lang.NullPointerException Thread-0 is TERMINATED } catch (InterruptedException e) { at System.out.println(e + " in main"); JoinTest$1.run(JoinTest.java:24) } main thread after join System.out.println(thread.getName() + " is " + thread.getState()); System.out.println("main thread after join");
  • 8. Interruption Interruption - is a signal for the thread that it should stop an execution. Each thread has to handle his own interruption. public void interrupt() - interrupts this thread. scenarios: - clears intrerrupt status, throws InterruptedException; - sets intrerrupt status; - does nothing. How to find out interrupt status? static boolean interrupted() boolean isInterrupted() clears interrupt status doesn't clear interrupt status Interruption processing example: if (Thread.interrupted()) { // do some work if needed throw new InterruptedException(); }
  • 9. Volatile There are several ways that can enhance performance: • storing data in registers • changing the order of operations Example: private boolean done = false; private volatile boolean done = false; public void run( ) { Begin method run Begin method run Load register r1 with memory Label L1: while (!done) { location 0Xff12345 Test if memory location 0xff12345 == foo( ); Label L1: 1 Test if register r1 == 1 If true branch to L2 } If true branch to L2 Call method foo Call method foo Branch to L1 } Branch to L1 Label L2: Label L2: End method End method run public void setDone( ) { done = true; } done is read only from register - done is read from main memory - there operations always will be looped is a chance to escape from loop Keyword volatile guaranties that value is never stored to registers. Synchronization has the similar effect because of registers' invalidation
  • 10. Changing operations' order We can not rely on operations' order to avoid costs of synchronization: Example: public int currentScore, totalScore, finalScore public void resetScore(boolean done) { finalScore = totalScore; currentScore = 0; currentScore = 0; finalScore = totalScore; totalScore += currentScore; if (done) { Thread1: Update total score Thread1: Update total score finalScore = totalScore; Thread2: See if currentScore == 0 Thread1: Set currentScore == 0 currentScore = 0; Thread2: Return -1 Thread2: See if currentScore == 0 } Thread1: Update finalScore Thread2: Return finalScore } Thread1: Set currentScore == 0 Thread1: Update finalScore public int getFinalScore( ) { logic is correct logic is corrupted if (currentScore == 0) return finalScore; return -1; } Synchronization protects operations from mixing: JVM cannot move operation outside the synchronization block. But operation that is located before or after synchronization block can be moved into synchronization block.
  • 11. Lock starvation Lock starvation occurs when a particular thread attempts to acquire a lock and never succeeds because another thread is already holding the lock. Reasons: The thread knows nothing about other contestants. The thread with higher priority is served first. A generous amount of threads. Synchronized blocks within loops often have this problem: while(true) { synchronized (this) { // execute some code } }
  • 12. ReentrantLock Solves lock starvation problem Construct a ReentrantLock object where the fairness flag is set to true. Threads are served very close to a first-come, first-served basis regardless of the thread's priority. Drawbacks: The priority can be set because the developer wanted to have the scheduler behave in a certain manner. ReentrantLock may change the desired scheduling behavior. fairness false fairness true Example: Thread-0 before sb Thread-1 before sb Thread-9 before sb Thread-2 before sb final ReentrantLock _lock = new ReentrantLock(true); Thread-3 before sb Thread-5 before sb Thread-7 before sb Thread-6 before sb @Override Thread-4 before sb Thread-6 in sb public void run() { Thread-6 before sb Thread-1 in sb System.out.println(Thread.currentThread().getName() + " before sb"); _lock.lock(); // will wait until this thread gets the lock Thread-5 before sb Thread-2 in sb try Thread-2 before sb Thread-5 in sb { Thread-1 before sb Thread-3 before sb System.out.println(Thread.currentThread().getName() + " in sb"); Thread-2 in sb Thread-3 in sb finally Thread-5 in sb Thread-7 before sb { Thread-6 in sb Thread-7 in sb //releasing the lock so that other threads can get notifies _lock.unlock(); Thread-4 in sb Thread-9 before sb } Thread-7 in sb Thread-9 in sb } Thread-3 in sb Thread-0 before sb Thread-9 in sb Thread-0 in sb Thread-0 in sb Thread-4 before sb Thread-1 in sb Thread-4 in sb
  • 13. Reader/Writer Locks ReentrantReadWriteLock class. Generally, reader/writer locks are used when there are many more readers than writers; The reader/writer lock is needed—to share data access during the long periods of time when only reading is needed. ReadLock - can be shared between readers WriteLock - exclusive work principle: preconditions: there are lots of writers and readers; behaviour (fairness = false) : Reader has finished task  Writer will be called; Writer has finished task  Reader or Writer will be called (arbitrary schedule); behaviour (fairness = true) : Threads are served as first-come, first-served basis
  • 14. ReentrantReadWriteLock example static class RRWLC { Result: ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); private final Lock read = readWriteLock.readLock(); Reader :Thread-7 is reading : sb = Reader :Thread-5 is reading : sb = private final Lock write = readWriteLock.writeLock(); Reader :Thread-0 is reading : sb = Reader :Thread-2 is reading : sb = StringBuilder sb = new StringBuilder(); Reader :Thread-3 is reading : sb = Reader :Thread-5 has finished reading Reader :Thread-3 has finished reading Reader :Thread-0 has finished reading public void read() { Reader :Thread-2 has finished reading Reader :Thread-7 has finished reading read.lock(); Writer :Thread-8 is writing Now sb = + try{ TimeUnit.SECONDS.sleep(2); //sout Writer :Thread-8 has finished writing Writer :Thread-9 is writing Now sb = + + } finally { read.unlock(); //sout } Writer :Thread-9 has finished writing Reader :Thread-1 is reading : sb = + + } Reader :Thread-4 is reading : sb = + + Reader :Thread-1 has finished reading Reader :Thread-4 has finished reading Writer :Thread-11 is writing Now sb = + + + public void write (){ Writer :Thread-11 has finished writing Reader :Thread-6 is reading : sb = + + + write.lock(); Reader :Thread-6 has finished reading Writer :Thread-10 is writing Now sb = + + + + try{ Writer :Thread-10 has finished writing System.out.println("Now sb = " + sb.append(" + "));//sout } finally{ write.unlock(); //sout } } } static class Reader extends Thread { static class Writer extends Thread { private RRWLC rrwlc; private RRWLC rrwlc; @Override @Override public void run() { ... rrwlc.read(); ...} public void run() { ... rrwlc.write(); ...} } }
  • 15. Thread Local Variables Thread local variable: - is private to particular thread; - cannot be used to share state between threads; - access to the variable need never be synchronized; If TLV has never been set before the initialValue() is called to return the value. Qualities Thread Local Local Object state independency + + Common monitor exists + - Lazy initialization + - disadvantages: Perfomance loses on chosing/getting object
  • 16. Thread Local example public class TestTreadLocal { public static void main(String[] args) { final ThreadLocal<MyObject> perThreadCounter = new ThreadLocal<MyObject>(); Thread t1 = new Thread() { Thread t2 = new Thread() { public void run() { public void run() { perThreadCounter.set(new MyObject(2)); perThreadCounter.set(new MyObject(3)); info(perThreadCounter); info(perThreadCounter); perThreadCounter.get().setX(8); perThreadCounter.get().setX(7); timeout(3); timeout(3); info(perThreadCounter); info(perThreadCounter); } } }; }; System.out.println("In main : " + perThreadCounter.get()); t1.start(); timeout(1); t2.start(); Result: } In main : null Thread-0 : java.lang.ThreadLocal@4ce86da0 : concurrency.TestTreadLocal2$MyObject@2f754ad2 : 2 Thread-1 : java.lang.ThreadLocal@4ce86da0 : concurrency.TestTreadLocal2$MyObject@95c7850 : 3 Thread-1 : java.lang.ThreadLocal@4ce86da0 : concurrency.TestTreadLocal2$MyObject@95c7850 : 7 Thread-0 : java.lang.ThreadLocal@4ce86da0 : concurrency.TestTreadLocal2$MyObject@2f754ad2 : 8
  • 17. Atomic operations and classes Allows operation that contains multiple instructions can be treated as atomic (for example ++) ; Classes: AtomicInteger, AtomicLong, AtomicBoolean, and AtomicReference. get() and set() - provide volatile functionality. getAndSet() - sets the new value returns the previous value. compareAndSet (exp, new) compare exp with current value. if they are equal - set new value, return true. weakCompareAndSet (exp, new) The AtomicInteger and AtomicLong additional methods: incrementAndGet(), getAndIncrement() - pre/post-increment decrementAndGet(), getAndDecrement() - pre/post-decrement addAndGet(), getAndAdd() - pre- and post-operators for the delta value.
  • 18. Atomic field updaters AtomicIntegerFieldUpdater, AtomicLongFieldUpdater, AtomicReferenceFieldUpdater can help to update volatile variables. These classes are abstract. Use the static newUpdater() method. Features: • Is used as "wrappers" around a volatile field; • Don't require adding an extra object for atomic access to your class; • Let refer to the variable "normally" (without get, set); To avoid using the synchronization via the atomic classes we have to make: • Simple variable substitution; • Changing algorithms; • Retrying operations;
  • 19. Implementation differences synchronized AtomicInteger AtomicIntegerFieldUpdater public class AtomicCounter { publiclass AtomicCounter { public class AtomicIntegerFieldUpdaterExample { int value = 0; private final AtomicInteger value = new public static void main(String[] args) { AtomicInteger(0); Counter c1 = new Counter(10); public synchronized void Counter c2 = new Counter(20); increment () { public int increment () { value++; return value.incrementAndGet(); AtomicIntegerFieldUpdater<Counter> iUpdater = } } AtomicIntegerFieldUpdater.newUpdater(Counter.class, // or via compareAndSet "value"); public void increment() { iUpdater.incrementAndGet(c1); while (true) { iUpdater.incrementAndGet(c2); int current = value.get(); System.out.println("c1.value = " + iUpdater.get(c1)); int next = current + 1; System.out.println("c2.value = " + iUpdater.get(c2)); } if (integer.compareAndSet(current, next)) { static class Counter{ return; } volatile int value; // private is not allowed } } Counter(int value) { this.value = value; } } }
  • 20. Semaphore Semaphore is a lock with a counter. Lets different threads acquire lock one or more times. Features: • A number of simultaneously executing threads have to be set in the constructor; • The acquire() and release() are similar to the lock() and unlock() methods of the Lock; • Supply fairness politics. • There is no concept of nesting;
  • 21. Semaphore example static class SemaphoreTester extends Thread { public class SemaphoreExample { public static void main(String[] args) { private Semaphore s; SemaphoreTester(Semaphore s) { this.s = s;} Semaphore sem = new Semaphore(3); @Override SemaphoreTester st1 = new SemaphoreTester(sem); public void run() { SemaphoreTester st2 = new SemaphoreTester(sem); try { st1.start(); System.out.println(getName() + " at first : is trying to acquire the lock "); st2.start(); s.acquire(); } System.out.println(getName() + " semaphore's first acquire has got"); System.out.println(getName() + " second time: is trying to acquire the lock " + TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())); s.acquire(); System.out.println(getName() + " semaphore's second acquire " + TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())); TimeUnit.SECONDS.sleep(5); Result: s.release(); System.out.println(getName() + " semaphore has been released"); Thread-1 at first : is trying to acquire the lock s.release(); Thread-1 semaphore's first acquire has got System.out.println(getName() + " semaphore has been released"); Thread-1 second time: is trying to acquire the lock 1351581890 } catch (InterruptedException e) { Thread-1 semaphore's second acquire 1351581890 e.printStackTrace(); lates. Thread-0 at first : is trying to acquire the lock } Thread-0 semaphore's first acquire has got Thread-0 second time: is trying to acquire the lock 1351581890 } Thread-1 semaphore has been released } Thread-1 semaphore has been released } Thread-0 semaphore's second acquire 1351581895 Thread-0 semaphore has been released Thread-0 semaphore has been released
  • 22. Barrier The barrier is simply a waiting point where all the threads can sync up. The last thread releases the barrier by notifying all of the other threads. stage 1 waiting stage 2 waiting stage 3 ... stage 1 waiting stage 2 waiting stage 3 ... stage 1 waiting stage 2 waiting stage 3 ... Barrier against join() benefits: we mustn't always create new threads; logical operations can be placed together; drawbacks: complex reinitialization process; Barrier can be broken when: 1. Waiting threads can be interrupted; 2. Thread may break through the barrier due to a timeout condition; 3. Exception could be thrown by the barrier action.
  • 23. CyclicBarrier example public class CyclicBarrierExample private static class Summer extends Thread { { int row; private static int matrix[][] = CyclicBarrier barrier; {{ 1 }, { 2, 2 }, { 3, 3, 3 }, { 4, 4, 4, 4 }, { 5, 5, 5, 5, 5 }}; private static int results[]; Summer(CyclicBarrier barrier, int row) { this.barrier = barrier; this.row = row; } public static void main(String args[]) { public void run() { final int rows = matrix.length; int columns = matrix[row].length; results = new int[rows]; int sum = 0; Runnable merger = new Runnable(){ for (int i = 0; i < columns; i++) public void run() sum += matrix[row][i]; { int sum = 0; results[row] = sum; for (int i = 0; i < rows; i++) System.out.println(Thread.currentThread().getName() + ": sum += results[i]; Results for row " + row + " are : " + sum); System.out.println("Merger :"+ " Results are: " + sum); }}; try{ barrier.await(); // wait for others System.out.println(Thread.currentThread().getName() + CyclicBarrier barrier = new CyclicBarrier(rows, merger); " do something else " + System.currentTimeMillis()); for (int i = 0; i < rows; i++) } catch ... { new Summer(barrier, i).start(); } }
  • 24. Result: Thread-0: Results for row 0 are : 1 Thread-1: Results for row 1 are : 4 Thread-2: Results for row 2 are : 9 Thread-4: Results for row 4 are : 25 Thread-3: Results for row 3 are : 16 Merger : Results are: 55 Thread-3 do something else 1351699717125 Thread-0 do something else 1351699717125 Thread-1 do something else 1351699717125 Thread-2 do something else 1351699717125 Thread-4 do something else 1351699717125
  • 26. References Books: Java Threads, Third Edition; Scott Oaks & Henry Wong; ISBN: 978-0-596-00782-9. O'Reilly, 2004. Concurrent Programming in Java: Design Principles and Patterns (2nd edition) Doug Lea; ISBN 0-201-31009-0. AddisonWesley, 1999. Web recources: http://www.javamex.com/tutorials/synchronization_concurrency_7_atomic_updaters.shtml http://www.baptiste-wicht.com/2010/09/java-concurrency-atomic-variables/ http://www.ibm.com/developerworks/java/library/j-jtp11234/ http://programmingexamples.wikidot.com/cyclicbarrier http://javarevisited.blogspot.com/2012/07/cyclicbarrier-example-java-5-concurrency-tutorial.html http://tutorials.jenkov.com/java-concurrency/semaphores.html http://alexbtw.livejournal.com/3355.html