public class Semaphore extends Object
acquire()
blocks until a permit is
available, whilst release()
will unblock it.Constructor and Description |
---|
Semaphore()
Creates a new semaphore that is available.
|
Semaphore(boolean isAvailable)
Creates a new semaphore and allows you to specify if it's available or not.
|
Modifier and Type | Method and Description |
---|---|
void |
acquire()
Acquires the semaphore, or blocks until it's available or the thread is interrupted.
|
void |
release()
Releases the semaphore.
|
boolean |
tryAcquire(long timeout)
Tries to acquire the semaphore and waits for the duration of the specified timeout
until it becomes available.
|
public Semaphore()
public Semaphore(boolean isAvailable)
isAvailable
- should the semaphore be available or notpublic void acquire() throws InterruptedException
InterruptedException
- when the thread is interruptedpublic boolean tryAcquire(long timeout) throws InterruptedException
timeout
- the number of milliseconds to waittrue
if the semaphore was acquired, false
if it was
not after waiting for the specified amount of timeInterruptedException
- when the thread is interruptedpublic void release()
Copyright © 2006–2016 The Apache Software Foundation. All rights reserved.