Class Semaphore
All Implemented Interfaces:
Declaration of Semaphore:
public class Semaphore
extends Object
implements Serializable
Java 5 comes with semaphore implementations in the java.util.concurrent package. Semaphores are used to restrict the number of threads than can access some resource. Semaphores are also used to send signals between two threads.
Semaphore class is a counting semaphore and it has two main methods:
- acquire()
- release()
The counting Semaphore are initialized with a given number of permits. Each acquire() blocks if necessary until a permit is available. For each call to acquire() a permit is taken by the calling thread. And For each call to release() a permit is returned to the semaphore. Thus at most N threads can pass the acquire() method without any release() calls, where N is number of permits the semaphore was initialized with. Here permits are just simple counter and nothing else.
The constructor for this class optionally accepts a fairness parameter. When set false, this class makes no guarantees about the order in which threads acquire permits. When fairness is set true, the semaphore guarantees that threads invoking any of the acquire methods are selected to obtain permits in the order in which their invocation of those methods was processed (first-in-first-out; FIFO).
Note that FIFO ordering necessarily applies to specific internal points of execution within these methods. So, it is possible for one thread to invoke acquire before another, but reach the ordering point after the other, and similarly upon return from the method. Also note that the untimed tryAcquire methods do not honor the fairness setting, but will take any permits that are available.
Constructor Summary of Semaphore:
Constructor
|
Description
|
Semaphore(int permits)
|
Creates a Semaphore with the given number of permits and nonfair
fairness setting.
|
Semaphore(int permits, boolean fair)
|
Creates a Semaphore with the given number of permits and the given
fairness setting.
|
Method Summary of Semaphore:
Modifier and Method Name
|
Description
|
void acquire()
|
Acquires a permit from this semaphore, blocking until one is
available, or the thread is interrupted.
|
void acquire(int permits)
|
Acquires the given number of permits from this semaphore, blocking until
all are available, or the thread is interrupted.
|
void acquireUninterruptibly()
|
Acquires a permit from this semaphore, blocking until one is
available.
|
void
acquireUninterruptibly(int permits)
|
Acquires the given number of permits from this semaphore, blocking
until all are available.
|
int availablePermits()
|
Returns the current number of permits available in this semaphore.
|
int drainPermits()
|
Acquires and returns all permits that are immediately available.
|
protected Collection<Thread> getQueuedThreads()
|
Returns a collection containing threads that may be waiting to
acquire.
|
int getQueueLength()
|
Returns an estimate of the number of threads waiting to acquire.
|
boolean hasQueuedThreads()
|
Queries whether any threads are waiting to acquire.
|
boolean isFair()
|
Returns true if this semaphore has fairness set true.
|
protected void
reducePermits(int reduction)
|
Shrinks the number of available permits by the indicated reduction.
|
void release()
|
Releases a permit, returning it to the semaphore.
|
void release(int permits)
|
Releases the given number of permits, returning them to the
semaphore.
|
String toString()
|
Returns a string identifying this semaphore, as well as its state.
|
boolean tryAcquire()
|
Acquires a permit from this semaphore, only if one is available at
the time of invocation.
|
boolean tryAcquire(int permits)
|
Acquires the given number of permits from this semaphore, only if all
are available at the time of invocation.
|
boolean
tryAcquire(int permits, long timeout, TimeUnit unit)
|
Acquires the given number of permits from this semaphore, if all
become available within the given waiting time and the current thread has not
been interrupted.
|
boolean
tryAcquire(long timeout, TimeUnit unit)
|
Acquires a permit from this semaphore, if one becomes available within
the given waiting time and the current thread has not been interrupted.
|
Example of Semaphore:
import java.util.concurrent.Semaphore;
public class SemaphoreClassExample {
private static final int CONCURRENT_THREADS = 2;
private final Semaphore semaphore = new Semaphore(CONCURRENT_THREADS, true);
public void starts() {
for (int i = 1; i <= 5; i++) {
Person person = new Person();
person.start();
}
}
class Person extends Thread {
@Override
public void run() {
try {
// Acquire Lock
semaphore.acquire();
} catch (InterruptedException e) {
System.out.println("received InterruptedException");
return;
}
System.out.println("Thread : " + this.getName() + " starts Acquire()");
try {
sleep(1000);
} catch (Exception e) {
} finally {
// Release Lock
semaphore.release();
}
System.out.println("Thread " + this.getName() + " stops Release()\n");
}
}
public static void main(String[] args) {
SemaphoreClassExample test = new SemaphoreClassExample();
test.starts();
}
}
Program Output:
Thread : Thread-1 starts Acquire()
Thread : Thread-3 starts Acquire()
Thread Thread-3 stops Release()
Thread Thread-1 stops Release()
Thread : Thread-0 starts Acquire()
Thread : Thread-2 starts Acquire()
Thread Thread-2 stops Release()
Thread Thread-0 stops Release()
Thread : Thread-4 starts Acquire()
Thread Thread-4 stops Release()
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Java I/O Tutorial

No comments:
Post a Comment