There's more...

You can find a situation where a requirement prevents you from getting the locks in the same order in all the operations. In this situation, you can use the tryLock() method of the Lock class. This method returns a Boolean value to indicate whether you have control of the lock. You can try to get all the locks that you need to do the operation using the tryLock() method. If you can't get control of one of the locks, you must release all the locks that you may have had and start the operation again.

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.