Java 8 From Smile To Tears: Emotional Stampedlock

Similar documents
From Smile To Tears: Emotional StampedLock

Phaser And StampedLock Concurrency Synchronizers

Using Java 8 Lambdas And Stampedlock To Manage Thread Safety

Principles of Software Construction: Concurrency, Part 2

Concurrent Red-Black Trees

G52CON: Concepts of Concurrency

JFokus Finding And Solving Java Deadlocks

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

CMSC 433 Programming Language Technologies and Paradigms Fall Locks & Conditions

The Secrets of Concurrency

Live Productive Coder

Buffer & File Optimization. Cloud Databases DataLab CS, NTHU

The Secrets of Concurrency. The Secrets of Concurrency

The Secrets of Concurrency

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Midterm 1, CSE 451, Winter 2001 (Prof. Steve Gribble)

The Secrets of Concurrency

DUBLIN CITY UNIVERSITY

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Principles of Software Construction: Concurrency, Part 2

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Lecture 6 Coordinating Resources

Synchronization in Concurrent Programming. Amit Gupta

The Secrets of Concurrency

CSE 332: Locks and Deadlocks. Richard Anderson, Steve Seitz Winter 2014

Multi-threaded Performance And Scalability

Dr Heinz M. Kabutz. The Secrets of Concurrency. The Java Specialists Newsletter.

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Concurrency in Object Oriented Programs 3. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

Deadlock Victim && && && && by Dr Heinz Kabutz The Java Specialists Newsletter

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CMSC 132: Object-Oriented Programming II

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

3/25/14. Lecture 25: Concurrency. + Today. n Reading. n P&C Section 6. n Objectives. n Concurrency

Threads and Java Memory Model

The Secrets of Concurrency

COMP 322: Fundamentals of Parallel Programming

Atomic Transac1ons. Atomic Transactions. Q1: What if network fails before deposit? Q2: What if sequence is interrupted by another sequence?

Advances in Programming Languages

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

CMSC 330: Organization of Programming Languages

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CMSC 132: Object-Oriented Programming II. Threads in Java

Object Oriented Programming and Design in Java. Session 18 Instructor: Bert Huang

Synchronization I. Jo, Heeseung

Readers/Writers Problem. Readers/Writers: Scenario 1. Readers/Writers Problem. Today: Synchronization for Readers/Writers Problem

POSIX / System Programming

CSE 153 Design of Operating Systems

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

CMSC 330: Organization of Programming Languages

COMP 322: Fundamentals of Parallel Programming. Lecture 30: Java Synchronizers, Dining Philosophers Problem

Models of concurrency & synchronization algorithms

Threads Chate Patanothai

Systems Fundamentals. SWE 622, Spring 2017 Distributed Software Engineering

CScheme in Traditional Concurrency Problems

Northeastern University - Seattle CS5510 Professor Ian Gorton

Problems with Concurrency. February 19, 2014

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

Programming in Java

Dealing with Issues for Interprocess Communication

Synchronization SPL/2010 SPL/20 1

RWLocks in Erlang/OTP

CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion. Ruth Anderson Winter 2019

Concurrency Control Algorithms

The New Java Technology Memory Model

Transaction Processing: Concurrency Control ACID. Transaction in SQL. CPS 216 Advanced Database Systems. (Implicit beginning of transaction)

Multiprocessor Systems. Chapter 8, 8.1

Multithreading. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

JAVA EXAMPLES - SOLVING DEADLOCK

Operating Systems (2INC0) 2017/18

CONCURRENT PROGRAMMING - EXAM

Programmazione di sistemi multicore

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II)

CSE 120 Principles of Operating Systems

SSC - Concurrency and Multi-threading Advanced topics about liveness

Kernel Range Reader/Writer Locking

CS 351 Design of Large Programs Threads and Concurrency

3C03 Concurrency: Starvation and Deadlocks

CSE332: Data Abstractions Lecture 23: Data Races and Memory Reordering Deadlock Readers/Writer Locks Condition Variables. Dan Grossman Spring 2012

Concurrency Utilities: JSR-166

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

CONCURRENT API AND PARALLEL PROGRAMMING

The Java Memory Model

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

What is a thread anyway?

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

ECE 587 Hardware/Software Co-Design Lecture 07 Concurrency in Practice Shared Memory I

Concurrent Programming Benoît Garbinato

MultiJav: A Distributed Shared Memory System Based on Multiple Java Virtual Machines. MultiJav: Introduction

Programming Paradigms for Concurrency Lecture 3 Concurrent Objects

6.852: Distributed Algorithms Fall, Class 20

User Space Multithreading. Computer Science, University of Warwick

Synchronized Methods of Old Versions of Java

Transcription:

!1 Java 8 From Smile To Tears: Emotional Stampedlock Dr Heinz M. Kabutz Last updated 2014-03-23

!2 Heinz Kabutz l Author of The Java Specialists' Newsletter Articles about advanced core Java programming l http://www.javaspecialists.eu

!3 Stampedlock

!4 Motivation For Stampedlock l Some constructs need a form of read/write lock l ReentrantReadWriteLock can cause starvation Plus it always uses pessimistic locking

!5 Motivation For Stampedlock l StampedLock provides optimistic locking on reads Which can be converted easily to a pessimistic read l Write locks are always pessimistic Also called exclusive locks l StampedLock is not reentrant

!6 Read-Write Locks Refresher l ReadWriteLock interface The writelock() is exclusive - only one thread at a time The readlock() is given to lots of threads at the same time Much better when mostly reads are happening Both locks are pessimistic

!7 Account With Reentrantreadwritelock public class BankAccountWithReadWriteLock { private final ReadWriteLock lock = new ReentrantReadWriteLock(); private double balance; public void deposit(double amount) { lock.writelock().lock(); try { balance = balance + amount; finally { lock.writelock().unlock(); public double getbalance() { lock.readlock().lock(); try { return balance; finally { lock.readlock().unlock(); The cost overhead of the RWLock means we need at least 2000 instructions to benefit from the readlock() added throughput

!8 Reentrantreadwritelock Starvation l When readers are given priority, then writers might never be able to complete (Java 5) l But when writers are given priority, readers might be starved (Java 6) l http://www.javaspecialists.eu/archive/issue165.html

!9 Java 5 Readwritelock Starvation l We first acquire some read locks l We then acquire one write lock l Despite write lock waiting, read locks are still issued l If enough read locks are issued, write lock will never get a chance and the thread will be starved!

!10 Readwritelock In Java 6 l Java 6 changed the policy and now read locks have to wait until the write lock has been issued l However, now the readers can be starved if we have a lot of writers

!11 Synchronized vs Reentrantlock l ReentrantReadWriteLock, ReentrantLock and synchronized locks have the same memory semantics l However, synchronized is easier to write correctly synchronized(this) { // do operation rwlock.writelock().lock(); try { // do operation finally { rwlock.writelock().unlock();

!12 Bad Try-Finally Blocks l Either no try-finally at all rwlock.writelock().lock(); // do operation rwlock.writelock().unlock();

!13 Bad Try-Finally Blocks l Or the lock is locked inside the try block try { rwlock.writelock().lock(); // do operation finally { rwlock.writelock().unlock();

!14 Bad Try-Finally Blocks l Or the unlock() call is forgotten in some places altogether! rwlock.writelock().lock(); // do operation // no unlock()

!15 Introducing Stampedlock l Pros Has better performance than ReentrantReadWriteLock Latest versions do not suffer from starvation of writers l Cons Idioms are more difficult than with ReadWriteLock A small change in idiom code can make a big difference in performance Not nonblocking Non-reentrant

!16 Pessimistic Exclusive Locks (Write) public class StampedLock { long writelock() long writelockinterruptibly() throws InterruptedException long trywritelock() long trywritelock(long time, TimeUnit unit) throws InterruptedException void unlockwrite(long stamp) boolean tryunlockwrite() Lock aswritelock() long tryconverttowritelock(long stamp)

!17 Pessimistic Non-Exclusive (Read) public class StampedLock { (continued ) long readlock() long readlockinterruptibly() throws InterruptedException long tryreadlock() long tryreadlock(long time, TimeUnit unit) throws InterruptedException void unlockread(long stamp) boolean tryunlockread() Lock asreadlock() long tryconverttoreadlock(long stamp) Optimistic reads to come...

!18 Bank Account With Stampedlock public class BankAccountWithStampedLock { private final StampedLock lock = new StampedLock(); private double balance; public void deposit(double amount) { long stamp = lock.writelock(); try { balance = balance + amount; finally { lock.unlockwrite(stamp); public double getbalance() { long stamp = lock.readlock(); try { return balance; finally { lock.unlockread(stamp); The StampedLock reading is a typically cheaper than ReentrantReadWriteLock

!19 Why Not Use Volatile? public class BankAccountWithVolatile { private volatile double balance;!! public synchronized void deposit(double amount) { balance = balance + amount; public double getbalance() { return balance; Much easier! Works because there are no invariants across the fields.

!20 Example With Invariants Across Fields l Point class has x,y coordinates, "belong together" public class MyPoint { private double x, y; private final StampedLock sl = new StampedLock();! // method is modifying x and y, needs exclusive lock public void move(double deltax, double deltay) { long stamp = sl.writelock(); try { x += deltax; y += deltay; finally { sl.unlockwrite(stamp);

!21!!!!!!!! Optimistic Non-Exclusive "Locks" public class StampedLock { Try to get an optimistic read long tryoptimisticread() lock - might return zero if an exclusive lock is active boolean validate(long stamp) checks whether a write lock was issued after the Note: sequence validation requires tryoptimisticread() was stricter ordering than apply to called normal volatile reads - a new explicit loadfence() was added long tryconverttooptimisticread(long stamp)

!22 Code Idiom For Optimistic Read public double optimisticread() { long stamp = sl.tryoptimisticread(); double currentstate1 = state1, currentstate2 = state2,... etc.; if (!sl.validate(stamp)) { stamp = sl.readlock(); try { currentstate1 = state1; currentstate2 = state2,... etc.; finally { sl.unlockread(stamp); return calculatesomething(currentstate1, currentstate2);

!23 Code Idiom For Optimistic Read public double optimisticread() { long stamp = sl.tryoptimisticread(); We get a double currentstate1 = state1, stamp to use currentstate2 = state2,... etc.; if (!sl.validate(stamp)) { for the stamp = sl.readlock(); optimistic try { read currentstate1 = state1; currentstate2 = state2,... etc.; finally { sl.unlockread(stamp); return calculatesomething(currentstate1, currentstate2);

!24 Code Idiom For Optimistic Read public double optimisticread() { long stamp = sl.tryoptimisticread(); double currentstate1 = state1, We read currentstate2 = state2,... etc.; field values if (!sl.validate(stamp)) { into local stamp = sl.readlock(); try { fields currentstate1 = state1; currentstate2 = state2,... etc.; finally { sl.unlockread(stamp); return calculatesomething(currentstate1, currentstate2);

!25 Code Idiom For Optimistic Read public double optimisticread() { long stamp = sl.tryoptimisticread(); double currentstate1 = state1, currentstate2 = state2,... etc.; if (!sl.validate(stamp)) { stamp = sl.readlock(); Next we validate try { that no write currentstate1 = state1; locks have been currentstate2 = state2,... etc.; finally { issued in the sl.unlockread(stamp); meanwhile return calculatesomething(currentstate1, currentstate2);

!26 Code Idiom For Optimistic Read If they have, public double optimisticread() { long stamp = sl.tryoptimisticread(); then we don't double currentstate1 = state1, currentstate2 = state2,... etc.; if (!sl.validate(stamp)) { is clean stamp = sl.readlock(); try { currentstate1 = state1; Thus we acquire a currentstate2 = state2,... etc.; finally { pessimistic read sl.unlockread(stamp); lock and read the state into local return calculatesomething(currentstate1, currentstate2); fields know if our state

!27 Code Idiom For Optimistic Read public double optimisticread() { long stamp = sl.tryoptimisticread(); double currentstate1 = state1, currentstate2 = state2,... etc.; if (!sl.validate(stamp)) { stamp = sl.readlock(); try { currentstate1 = state1; currentstate2 = state2,... etc.; finally { sl.unlockread(stamp); return calculatesomething(currentstate1, currentstate2);

!28 Optimistic Read In Point Class public double distancefromorigin() { long stamp = sl.tryoptimisticread(); double currentx = x, currenty = y; if (!sl.validate(stamp)) { stamp = sl.readlock(); try { currentx = x; currenty = y; finally { sl.unlockread(stamp); return Math.hypot(currentX, currenty); Shorter code path in optimistic read leads to better read performance than with original examples in JavaDoc

!29 Code Idiom For Conditional Change public boolean changestateifequals(oldstate1, oldstate2,... newstate1, newstate2,...) { long stamp = sl.readlock(); try { while (state1 == oldstate1 && state2 == oldstate2...) { long writestamp = sl.tryconverttowritelock(stamp); if (writestamp!= 0L) { stamp = writestamp; state1 = newstate1; state2 = newstate2;... return true; else { sl.unlockread(stamp); stamp = sl.writelock(); return false; finally { sl.unlock(stamp);

!30 Code Idiom For Conditional Change public boolean changestateifequals(oldstate1, oldstate2,... newstate1, newstate2,...) { long stamp = sl.readlock(); try { while (state1 == oldstate1 && state2 == oldstate2...) { long writestamp = sl.tryconverttowritelock(stamp); if (writestamp!= 0L) { stamp = writestamp; state1 = newstate1; state2 = newstate2;... return true; else { sl.unlockread(stamp); stamp = sl.writelock(); We get a pessimistic read lock return false; finally { sl.unlock(stamp);

!31 Code Idiom For Conditional Change public boolean changestateifequals(oldstate1, oldstate2,... newstate1, newstate2,...) { long stamp = sl.readlock(); try { while (state1 == oldstate1 && state2 == oldstate2...) { long writestamp = sl.tryconverttowritelock(stamp); if (writestamp!= 0L) { stamp = writestamp; state1 = newstate1; state2 = newstate2;... return true; else { sl.unlockread(stamp); stamp = sl.writelock(); return false; finally { sl.unlock(stamp); If the state is not the expected state, we unlock and exit method Note: the general unlock() method can unlock both read and write locks

!32 Code Idiom For Conditional Change public boolean changestateifequals(oldstate1, oldstate2,... newstate1, newstate2,...) { long stamp = sl.readlock(); try { while (state1 == oldstate1 && state2 == oldstate2...) { long writestamp = sl.tryconverttowritelock(stamp); if (writestamp!= 0L) { stamp = writestamp; state1 = newstate1; state2 = newstate2;... return true; else { sl.unlockread(stamp); stamp = sl.writelock(); return false; finally { sl.unlock(stamp); We try convert our read lock to a write lock

!33 Code Idiom For Conditional Change public boolean changestateifequals(oldstate1, oldstate2,... newstate1, newstate2,...) { long stamp = sl.readlock(); try { while (state1 == oldstate1 && state2 == oldstate2...) { long writestamp = sl.tryconverttowritelock(stamp); if (writestamp!= 0L) { stamp = writestamp; state1 = newstate1; state2 = newstate2;... return true; else { sl.unlockread(stamp); stamp = sl.writelock(); return false; finally { sl.unlock(stamp); If we are able to upgrade to a write lock (ws!= 0L), we change the state and exit

!34 Code Idiom For Conditional Change public boolean changestateifequals(oldstate1, oldstate2,... newstate1, newstate2,...) { long stamp = sl.readlock(); try { while (state1 == oldstate1 && state2 == oldstate2...) { long writestamp = sl.tryconverttowritelock(stamp); if (writestamp!= 0L) { stamp = writestamp; state1 = newstate1; state2 = newstate2;... return true; else { sl.unlockread(stamp); stamp = sl.writelock(); return false; finally { sl.unlock(stamp); Else, we explicitly unlock the read lock and lock the write lock And we try again

!35 Code Idiom For Conditional Change public boolean changestateifequals(oldstate1, oldstate2,... newstate1, newstate2,...) { long stamp = sl.readlock(); try { while (state1 == oldstate1 && state2 == oldstate2...) { long writestamp = sl.tryconverttowritelock(stamp); if (writestamp!= 0L) { stamp = writestamp; state1 = newstate1; state2 = newstate2;... expected state, we return true; else { sl.unlockread(stamp); stamp = sl.writelock(); This could happen if between the unlockread() and the writelock() return false; finally { sl.unlock(stamp); another thread changed the values If the state is not the unlock and exit method

!36 Code Idiom For Conditional Change Because we hold the write lock, public boolean changestateifequals(oldstate1, oldstate2,... newstate1, newstate2,...) { long stamp = sl.readlock(); method will succeed try { while (state1 == oldstate1 && state2 == oldstate2...) { long writestamp = sl.tryconverttowritelock(stamp); if (writestamp!= 0L) { stamp = writestamp; state1 = newstate1; state2 = newstate2;... return true; else { sl.unlockread(stamp); We update the state and exit stamp = sl.writelock(); return false; finally { sl.unlock(stamp); the tryconverttowritelock()

!37 Code Idiom For Conditional Change public boolean changestateifequals(oldstate1, oldstate2,... newstate1, newstate2,...) { long stamp = sl.readlock(); try { while (state1 == oldstate1 && state2 == oldstate2...) { long writestamp = sl.tryconverttowritelock(stamp); if (writestamp!= 0L) { stamp = writestamp; state1 = newstate1; state2 = newstate2;... return true; else { sl.unlockread(stamp); stamp = sl.writelock(); return false; finally { sl.unlock(stamp);

!38 Applying To Our Point Class public boolean moveifat(double oldx, double oldy, double newx, double newy) { long stamp = sl.readlock(); try { while (x == oldx && y == oldy) { long writestamp = sl.tryconverttowritelock(stamp); if (writestamp!= 0L) { stamp = writestamp; x = newx; y = newy; return true; else { sl.unlockread(stamp); stamp = sl.writelock(); return false; finally { sl.unlock(stamp);

!39 Performance Stampedlock & Rwlock l We researched ReentrantReadWriteLock in 2008 Discovered serious starvation of writers (exclusive lock) in Java 5 And also some starvation of readers in Java 6 http://www.javaspecialists.eu/archive/issue165.html l StampedLock released to concurrency-interest list 12 th Oct 2012 Worse writer starvation than in the ReentrantReadWriteLock Missed signals could cause StampedLock to deadlock l Revision 1.35 released 28 th Jan 2013 Changed to use an explicit call to loadfence() Writers do not get starved anymore Works correctly

!40 Performance Stampedlock & Rwlock l In our test, we used lambda-8-b75-linux-x64-28_jan_2013.tar.gz Two CPUs, 4 Cores each, no hyperthreading 2x4x1 Ubuntu 9.10 64-bit Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz L1-Cache: 256KiB, internal write-through instruction L2-Cache: 1MiB, internal write-through unified L3-Cache: 8MiB, internal write-back unified JavaSpecialists.eu server Never breaks a sweat delivering newsletters

!41 Conversions To Pessimistic Reads l In our experiment, reads had to be converted to pessimistic reads less than 10% of the time And in most cases, less than 1% l This means the optimistic read worked most of the time

From Smile to Tears: Emotional StampedLock x faster than ReadWriteLock l With a single thread 5 Read Speedup Write Speedup 4.43x 4 3 1.08x 1 0 0.00x R=1,W=0 0.00x R=0,W=1 2013-2014 Heinz Kabutz All Rights Reserved How Much Faster Is Stampedlock Than Reentrantreadwritelock?!42

From Smile to Tears: Emotional StampedLock!43 l With four threads x faster than ReadWriteLock 1000 100 10 1 353x 64x 11x Read Speedup Write Speedup 12x 1.2x 1.1x 1.2x 0.9x 0 R=4,W=0 R=3,W=1 R=2,W=2 R=1,W=3 R=0,W=4 2013-2014 Heinz Kabutz All Rights Reserved How Much Faster Is Stampedlock Than Reentrantreadwritelock?

From Smile to Tears: Emotional StampedLock!44 x faster than ReadWriteLock l With sixteen threads 10000 This demonstrates the starvation problem on readers in RWLock 1000 100 10 1 R=16,W=0 R=13,W=3 R=10,W=6 R=7,W=9 R=4,W=12 R=1,W=15 2013-2014 Heinz Kabutz All Rights Reserved How Much Faster Is Stampedlock Than Reentrantreadwritelock? Read Speedup Write Speedup

!45 Reader Throughput With Stampedlock 2013-2014 Heinz Kabutz All Rights Reserved Throughput (Logarithmic Scale) From Smile to Tears: Emotional StampedLock Read Throughput Expected (linear to n cores) 10000 1000 100 1 2 4 8 16 Number of Reader Threads (no Writers)

!46 Writer Throughput With Stampedlock Note: Linear Scale throughput Throughput (Linear Scale) 2.0 1.5 1.0 0.5 0.0 1 2 4 8 16 Number of Writer Threads (no Readers) Write Throughput

!47 Mixed Reader Throughput Stampedlock Throughput (Logarithmic Scale) 10000 1000 100 10 1 Read Throughput 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Number of Reader Threads (16 - n Writers)

!48 Mixed Reader Throughput Rwlock Throughput (Logarithmic Scale) 100 10 1 0.1 0.01 0.001 ReentrantReadWriteLock 16151413121110 9 8 7 6 5 4 3 2 1 Number of Reader Threads (16 - n Writers) Read Throughput Shows Reader Starvation in RWLock

!49 Conclusion Of Performance Analysis l StampedLock performed very well in all our tests Much faster than ReentrantReadWriteLock l Offers a way to do optimistic locking in Java l Good idioms have a big impact on the performance

!50 Idioms With Lambdas

!51 Idioms With Lambdas l Java 8 lambdas allow us to define a structure of a method, leaving the details of what to call over to users A bit like the "Template Method" Design Pattern List<String> students = new ArrayList<>(); Collections.addAll(students, "Anton", "Heinz", "John"); students.foreach((s) -> System.out.println(s.toUpperCase())); ANTON HEINZ JOHN

!52 Lambdafaq.Org l Edited by Maurice Naftalin Are lambda expressions objects? Why are lambda expressions so-called? Why are lambda expressions being added to Java? Where is the Java Collections Framework going? Why are Stream operations not defined directly on Collection? etc.

!53 Idioms For Using Stampedlock import java.util.concurrent.locks.*; import java.util.function.*;! public class LambdaStampedLock extends StampedLock { public void writelock(runnable writejob) { long stamp = writelock(); try { writejob.run(); finally { sl.unlockwrite(stamp); lsl.writelock( () -> { x += deltax; y += deltay; );

!54 Idioms For Using Stampedlock public <T> T optimisticread(supplier<t> supplier) { long stamp = tryoptimisticread(); T result = supplier.get(); if (!validate(stamp)) { stamp = readlock(); try { result = supplier.get(); finally { unlockread(stamp); return result; double[] xy = lsl.optimisticread( () -> new double[]{x, y ); return Math.hypot(xy[0], xy[1]);

!55 Idioms For Using Stampedlock public static boolean conditionalwrite( BooleanSupplier condition, Runnable action) { long stamp = readlock(); try { while (condition.getasboolean()) { long writestamp = tryconverttowritelock(stamp); if (writestamp!= 0) { action.run(); stamp = writestamp; return true; else { unlockread(stamp); stamp = writelock(); return false; finally { unlock(stamp); return lsl.conditionalwrite( () -> x == oldx && y == oldy, () -> { x = newx; y = newy; );

!56 From Smile To Tears: Emotional Stampedlock heinz@javaspecialists.eu Questions?

!57 The Java Specialists' Newsletter