O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

There's more...

The Lock interface (and the ReentrantLock class) includes another method to get control of the lock. It's the tryLock() method. The biggest difference with the lock() method is that this method, if the thread that uses it can't get control of the Lock interface, returns immediately and doesn't put the thread to sleep. It returns the boolean value true if the thread gets control of the lock and false if not. You can also pass a time value and a TimeUnit object to indicate the maximum amount of time the thread will wait to get the lock. If the time elapses and the thread doesn't get the lock, the method will return the false value. The TimeUnit class is an enumeration with the following constants: DAYS, HOURS, MICROSECONDS

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required