There's more...

The Semaphore class has three additional versions of the acquire() method:

  • acquireUninterruptibly(): The acquire() method, when the internal counter of the semaphore is 0, blocks the thread until the semaphore is released. During this period, the thread may be interrupted; if this happens, the method will throw an InterruptedException exception. This version of the acquire operation ignores the interruption of the thread and doesn't throw any exceptions.
  • tryAcquire(): This method tries to acquire the semaphore. If it can, it returns the true value. But if it can't, it returns false instead of being blocked and waits for the release of the semaphore. It's your responsibility to take correct action based on the return value. ...

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.