How it works...

The Java Concurrency API provides a class that can be used to implement synchronization mechanisms with features of locks or semaphores. It's called AbstractQueuedSynchronizer, and as the name suggests, it's an abstract class. It provides operations to control access to a critical section and manage a queue of threads that are blocked and are awaiting access to the section. The operations are based on two abstract methods:

  • tryAcquire(): This method is called to try and get access to a critical section. If the thread that calls it can access the critical section, the method returns the true value. Otherwise, it returns the false value.
  • tryRelease(): This method is called to try and release access to a critical section. If ...

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.