Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Managed Runtime Systems: Lecture 10 - Java Conc...

zakkak
April 16, 2018

Managed Runtime Systems: Lecture 10 - Java Concurrency

zakkak

April 16, 2018
Tweet

More Decks by zakkak

Other Decks in Programming

Transcript

  1. Managed Runtime Systems Lecture 10: Java Concurrency Foivos Zakkak https://foivos.zakkak.net

    Except where otherwise noted, this presentation is licensed under the Creative Commons Attribution 4.0 International License. Third party marks and brands are the property of their respective holders.
  2. Java Concurrency Built-in Through standard libraries Through third party libraries

    Managed Runtime Systems 1 of 24 https://foivos.zakkak.net
  3. Built-in Java Concurrency Synchronized methods: public synchronized void foo() ...

    Synchronized block: synchronized(barObject) ... Volatile variables: volatile int volatileInteger; Java Threads: myThread.start(); myThread.join(); ... Object wait/notify: myObject.wait(); myObject.notify(); ... Managed Runtime Systems 2 of 24 https://foivos.zakkak.net
  4. Java Threads Can be started only once Can have different

    priorities 1-10, default is 5 See also: Thread.MIN_PRIORITY , Thread.NORMAL_PRIORITY , Thread.MAX_PRIORITY However, priorities are not guaranteed to be taken into account Managed Runtime Systems 3 of 24 https://foivos.zakkak.net
  5. Java Threads Thread.sleep(duration) Stop for duration milliseconds Thread.yield() Let the

    JVM know that we are willing to stop running to allow other threads to run myThread.join() Pause current thread till the completion of myThread (it can also be given a timeout as argument) Managed Runtime Systems 4 of 24 https://foivos.zakkak.net
  6. Java Objects myObject.wait() Pause execution till notified (it can also

    be given a timeout as argument). Wait implicitly releases any locks owned by current thread and re-acquires them on notification. myObject.notify() Notify one of the waiting threads to resume execution myObject.notifyAll() Notify all the waiting threads Managed Runtime Systems 5 of 24 https://foivos.zakkak.net
  7. Be Careful Though! public Object getLast(Vector vector) { int lastIndex

    = vector.size() - 1; return vector.get(lastIndex); } public void deleteLast(Vector vector) { int lastIndex = vector.size() - 1; list.remove(lastIndex); } Managed Runtime Systems 8 of 24 https://foivos.zakkak.net
  8. Be Careful Though! public Object getLast(Vector vector) { synchronized (vector)

    { int lastIndex = vector.size() - 1; return vector.get(lastIndex); } } public void deleteLast(Vector vector) { synchronized (vector) { int lastIndex = vector.size() - 1; list.remove(lastIndex); } } Managed Runtime Systems 9 of 24 https://foivos.zakkak.net
  9. Concurrent Collections ConcurrentMap : set with no duplicates ConcurrentHashMap :

    hash-based set with no duplicates BlockingQueue : producer-consumer CopyOnWriteArrayList : optimized synchronized list Managed Runtime Systems 13 of 24 https://foivos.zakkak.net
  10. CountDownLatch Blocks threads that invoke await() Threads unblock when latch

    reaches zero through countDown() Managed Runtime Systems 14 of 24 https://foivos.zakkak.net
  11. Semaphore A counting semaphore, allowing a fixed number of threads

    to proceed Managed Runtime Systems 15 of 24 https://foivos.zakkak.net
  12. CyclicBarrier Blocks till a number of threads reach the barrier

    and then continues Managed Runtime Systems 16 of 24 https://foivos.zakkak.net
  13. Phaser Like CyclicBarrier but the number of threads that need

    to reach the barrier can change over time Managed Runtime Systems 17 of 24 https://foivos.zakkak.net
  14. Exchanger Block till another thread reaches the exchange point and

    exchange an object Managed Runtime Systems 18 of 24 https://foivos.zakkak.net
  15. Executor Framework Create and manage tasks Various thread pools to

    schedule executors on Managed Runtime Systems 19 of 24 https://foivos.zakkak.net
  16. Futures Tasks that return a value We can see if

    a Future is complete and get its value We can wait a Future We can cancel a Future Managed Runtime Systems 20 of 24 https://foivos.zakkak.net
  17. Memory Model 1/2 The methods of all classes in java.util.concurrent

    and its subpackages extend these guarantees to higher-level synchronization. In particular: ▪ Actions in a thread prior to placing an object into any concurrent collection happen-before actions subsequent to the access or removal of that element from the collection in another thread ▪ Actions in a thread prior to the submission of a Runnable to an Executor happen-before its execution begins. Similarly for Callables submitted to an ExecutorService ▪ Actions taken by the asynchronous computation represented by a Future happen-before actions subsequent to the retrieval of the result via Future.get() in another thread Managed Runtime Systems 22 of 24 https://foivos.zakkak.net
  18. Memory Model 2/2 ▪ Actions prior to ”releasing” synchronizer methods

    such as Lock.unlock, Semaphore.release, and CountDownLatch.countDown happen-before actions subsequent to a successful ”acquiring” method such as Lock.lock, Semaphore.acquire, Condition.await, and CountDownLatch.await on the same synchronizer object in another thread ▪ For each pair of threads that successfully exchange objects via an Exchanger, actions prior to the exchange() in each thread happen-before those subsequent to the corresponding exchange() in another thread ▪ Actions prior to calling CyclicBarrier.await and Phaser.awaitAdvance (as well as its variants) happen-before actions performed by the barrier action, and actions performed by the barrier action happen-before actions subsequent to a successful return from the corresponding await in other threads Managed Runtime Systems 23 of 24 https://foivos.zakkak.net