Slide 1

Slide 1 text

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.

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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