A semaphore controls access to a shared resource through the use
of a counter. If the counter is greater than zero, then access is
allowed. If it is zero, then access is denied. What the counter is
counting are permits that allow access to the shared resource. Thus, to
access the resource, a thread must be granted a permit from the
semaphore.
Working of semaphore
In general, to use a semaphore, the thread that wants access to the shared resource tries to acquire a permit.
- If the semaphore’s count is greater than zero, then the thread
acquires a permit, which causes the semaphore’s count to be decremented.
- Otherwise, the thread will be blocked until a permit can be acquired.
- When the thread no longer needs an access to the shared resource, it
releases the permit, which causes the semaphore’s count to be
incremented.
- If there is another thread waiting for a permit, then that thread will acquire a permit at that time.
Java provide Semaphore class in java.util.concurrent package that implements this mechanism, so you don’t have to implement your own semaphores.
Constructors in Semaphore class : There are two constructors in Semaphore class.
Semaphore(int num)
Semaphore(int num, boolean how)
Here, num specifies the initial permit count. Thus, it
specifies the number of threads that can access a shared resource at any
one time. If it is one, then only one thread can access the resource at
any one time. By default, all waiting threads are granted a permit in
an undefined order. By setting how to true, you can ensure that waiting threads are granted a permit in the order in which they requested access.
Using Semaphores as Locks(preventing race condition)
We can use a semaphore to lock access to a resource, each thread that wants to use that resource must first call acquire( ) before accessing the resource to acquire the lock. When the thread is done with the resource, it must call release( ) to release lock. Here is an example that demonstrate this:
import
java.util.concurrent.*;
class
Shared
{
static
int
count =
0
;
}
class
MyThread
extends
Thread
{
Semaphore sem;
String threadName;
public
MyThread(Semaphore sem, String threadName)
{
super
(threadName);
this
.sem = sem;
this
.threadName = threadName;
}
@Override
public
void
run() {
if
(
this
.getName().equals(
"A"
))
{
System.out.println(
"Starting "
+ threadName);
try
{
System.out.println(threadName +
" is waiting for a permit."
);
sem.acquire();
System.out.println(threadName +
" gets a permit."
);
for
(
int
i=
0
; i <
5
; i++)
{
Shared.count++;
System.out.println(threadName +
": "
+ Shared.count);
Thread.sleep(
10
);
}
}
catch
(InterruptedException exc) {
System.out.println(exc);
}
System.out.println(threadName +
" releases the permit."
);
sem.release();
}
else
{
System.out.println(
"Starting "
+ threadName);
try
{
System.out.println(threadName +
" is waiting for a permit."
);
sem.acquire();
System.out.println(threadName +
" gets a permit."
);
for
(
int
i=
0
; i <
5
; i++)
{
Shared.count--;
System.out.println(threadName +
": "
+ Shared.count);
Thread.sleep(
10
);
}
}
catch
(InterruptedException exc) {
System.out.println(exc);
}
System.out.println(threadName +
" releases the permit."
);
sem.release();
}
}
}
public
class
SemaphoreDemo
{
public
static
void
main(String args[])
throws
InterruptedException
{
Semaphore sem =
new
Semaphore(
1
);
MyThread mt1 =
new
MyThread(sem,
"A"
);
MyThread mt2 =
new
MyThread(sem,
"B"
);
mt1.start();
mt2.start();
mt1.join();
mt2.join();
System.out.println(
"count: "
+ Shared.count);
}
}