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

Managed Runtime Systems: Lecture 10 - Java Concurrency

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.

    View full-size slide

  2. Java Concurrency
    Built-in
    Through standard libraries
    Through third party libraries
    Managed Runtime Systems 1 of 24 https://foivos.zakkak.net

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  7. Standard Libraries
    Vector and Hashtable are thread-safe
    https://docs.oracle.com/javase/8/docs/api/java/util/
    Vector.html https://docs.oracle.com/javase/8/docs/api/
    java/util/Hashtable.html
    Managed Runtime Systems 6 of 24 https://foivos.zakkak.net

    View full-size slide

  8. Standard Libraries
    Thread-safe Collections:
    java.util.Collections.synchronized*
    https://docs.oracle.com/javase/8/docs/api/java/util/
    Collections.html
    Managed Runtime Systems 7 of 24 https://foivos.zakkak.net

    View full-size slide

  9. 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

    View full-size slide

  10. 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

    View full-size slide

  11. Standard Libraries
    Atomic variables: java.util.concurrent.atomic
    https://docs.oracle.com/javase/8/docs/api/java/util/
    concurrent/atomic/package-summary.html
    Managed Runtime Systems 10 of 24 https://foivos.zakkak.net

    View full-size slide

  12. Standard Libraries
    More locks: java.util.concurrent.locks
    https://docs.oracle.com/javase/8/docs/api/java/util/
    concurrent/locks/package-summary.html
    Managed Runtime Systems 11 of 24 https://foivos.zakkak.net

    View full-size slide

  13. Standard Libraries
    Concurrent Collections: java.util.concurrent
    https://docs.oracle.com/javase/8/docs/api/java/util/
    concurrent/package-summary.html
    Managed Runtime Systems 12 of 24 https://foivos.zakkak.net

    View full-size slide

  14. 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

    View full-size slide

  15. CountDownLatch
    Blocks threads that invoke await()
    Threads unblock when latch reaches zero through countDown()
    Managed Runtime Systems 14 of 24 https://foivos.zakkak.net

    View full-size slide

  16. Semaphore
    A counting semaphore, allowing a fixed number of threads to
    proceed
    Managed Runtime Systems 15 of 24 https://foivos.zakkak.net

    View full-size slide

  17. CyclicBarrier
    Blocks till a number of threads reach the barrier and then continues
    Managed Runtime Systems 16 of 24 https://foivos.zakkak.net

    View full-size slide

  18. 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

    View full-size slide

  19. Exchanger
    Block till another thread reaches the exchange point and exchange
    an object
    Managed Runtime Systems 18 of 24 https://foivos.zakkak.net

    View full-size slide

  20. Executor Framework
    Create and manage tasks
    Various thread pools to schedule executors on
    Managed Runtime Systems 19 of 24 https://foivos.zakkak.net

    View full-size slide

  21. 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

    View full-size slide

  22. More!!!
    https://docs.oracle.com/javase/8/docs/api/java/util/
    concurrent/package-summary.html
    Managed Runtime Systems 21 of 24 https://foivos.zakkak.net

    View full-size slide

  23. 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

    View full-size slide

  24. 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

    View full-size slide

  25. Third Party Libraries
    Threadly (https://threadly.github.io/threadly/)
    JCTools (https://github.com/JCTools/JCTools)
    ...
    Managed Runtime Systems 24 of 24 https://foivos.zakkak.net

    View full-size slide