SlideShare una empresa de Scribd logo
1 de 26
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

Más contenido relacionado

La actualidad más candente

Java 5 concurrency
Java 5 concurrencyJava 5 concurrency
Java 5 concurrency
priyank09
 
13multithreaded Programming
13multithreaded Programming13multithreaded Programming
13multithreaded Programming
Adil Jafri
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Carol McDonald
 
.NET Multithreading and File I/O
.NET Multithreading and File I/O.NET Multithreading and File I/O
.NET Multithreading and File I/O
Jussi Pohjolainen
 
Advanced Java Practical File
Advanced Java Practical FileAdvanced Java Practical File
Advanced Java Practical File
Soumya Behera
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
Alok Kumar
 

La actualidad más candente (20)

Java 5 concurrency
Java 5 concurrencyJava 5 concurrency
Java 5 concurrency
 
04 threads
04 threads04 threads
04 threads
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Threads
ThreadsThreads
Threads
 
13multithreaded Programming
13multithreaded Programming13multithreaded Programming
13multithreaded Programming
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
 
.NET Multithreading and File I/O
.NET Multithreading and File I/O.NET Multithreading and File I/O
.NET Multithreading and File I/O
 
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coinsoft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Advanced Java Practical File
Advanced Java Practical FileAdvanced Java Practical File
Advanced Java Practical File
 
Java Programming - 03 java control flow
Java Programming - 03 java control flowJava Programming - 03 java control flow
Java Programming - 03 java control flow
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
 
Play image
Play imagePlay image
Play image
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Jist of Java
Jist of JavaJist of Java
Jist of Java
 
JAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICESJAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICES
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
sizeof(Object): how much memory objects take on JVMs and when this may matter
sizeof(Object): how much memory objects take on JVMs and when this may mattersizeof(Object): how much memory objects take on JVMs and when this may matter
sizeof(Object): how much memory objects take on JVMs and when this may matter
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
Clojure: a LISP for the JVM
Clojure: a LISP for the JVMClojure: a LISP for the JVM
Clojure: a LISP for the JVM
 

Similar a Java concurrency begining

Dead locks9cm604.39
Dead locks9cm604.39Dead locks9cm604.39
Dead locks9cm604.39
myrajendra
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
Pramod Kumar
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
myrajendra
 
Google: Cluster computing and MapReduce: Introduction to Distributed System D...
Google: Cluster computing and MapReduce: Introduction to Distributed System D...Google: Cluster computing and MapReduce: Introduction to Distributed System D...
Google: Cluster computing and MapReduce: Introduction to Distributed System D...
tugrulh
 

Similar a Java concurrency begining (20)

Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
concurrency_c#_public
concurrency_c#_publicconcurrency_c#_public
concurrency_c#_public
 
Java synchronizers
Java synchronizersJava synchronizers
Java synchronizers
 
Thread
ThreadThread
Thread
 
Inheritance
InheritanceInheritance
Inheritance
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
Java programming PPT. .pptx
Java programming PPT.                 .pptxJava programming PPT.                 .pptx
Java programming PPT. .pptx
 
Concurrency gotchas
Concurrency gotchasConcurrency gotchas
Concurrency gotchas
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Dead locks9cm604.39
Dead locks9cm604.39Dead locks9cm604.39
Dead locks9cm604.39
 
Java Multithreading.pptx
Java Multithreading.pptxJava Multithreading.pptx
Java Multithreading.pptx
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
chap 7 : Threads (scjp/ocjp)
chap 7 : Threads (scjp/ocjp)chap 7 : Threads (scjp/ocjp)
chap 7 : Threads (scjp/ocjp)
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Google: Cluster computing and MapReduce: Introduction to Distributed System D...
Google: Cluster computing and MapReduce: Introduction to Distributed System D...Google: Cluster computing and MapReduce: Introduction to Distributed System D...
Google: Cluster computing and MapReduce: Introduction to Distributed System D...
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Java interface
Java interfaceJava interface
Java interface
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

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