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.
  • tryAcquire(long timeout, TimeUnit unit): This method is equivalent to the previous one, but it waits for the semaphore for the period of time specified in the parameters. If the period of time ends and the method hasn't acquired the semaphore, it will return false.

The acquire(), acquireUninterruptibly(), tryAcquire(), and release() methods have an additional version, which has an int parameter. This parameter represents the number of permits the thread that uses them wants to acquire or release, in other words, the number of units that this thread wants to delete or add to the internal counter of the semaphore.

In the case of the acquire(), acquireUninterruptibly(), and tryAcquire() methods, if the value of the counter is less than the number passed as parameter value, the thread will be blocked until the counter gets the same value or a greater one.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.231.15