Semaphore
·
A
semaphore restricts the number of simultaneous users of a shared resource up to
a maximum number. Threads can request access to the resource (decrementing the
semaphore), and can signal that they have finished using the resource
(incrementing the semaphore).
·
Counting
Semaphore in java maintains specified number of pass or permits
·
In
order to access the shared resource, current thread must acquire a permit
· If
permit is already exhausted by other thread then it can wait until a permit is
available due to release of permit from different thread
· java.util.Semaphore
class represent a Counting semaphore which is initialized with number of
permits.
· Semaphore
provides two main method acquire() and release() for getting permits and
releasing permits.
·
A
Counting semaphore with one permit is known as binary semaphore because it has
only two state permit available or permit unavailable.
· They
are able to make threads wait when counter value is zero i.e. they act as Locks
with counter functionality.
· When
a thread has finished the use of the shared resource, it must release the
semaphore so that the other threads can access the shared resource. That
operation increases the internal counter of the semaphore.
Example
import java.util.concurrent.Semaphore;
public class SemaphoreTest {
Semaphore binary = new Semaphore(1);
public static void main(String args[]) {
final SemaphoreTest test = new SemaphoreTest();
new Thread(){
@Override
public void run(){
test.mutualExclusion();
}
}.start();
new Thread(){
@Override
public void run(){
test.mutualExclusion();
}
}.start();
}
private void mutualExclusion() {
try {
binary.acquire();
//mutual exclusive region
System.out.println(Thread.currentThread().getName() + " inside mutual exclusive region");
Thread.sleep(1000);
} catch (InterruptedException i.e.) {
ie.printStackTrace();
} finally {
binary.release();
System.out.println(Thread.currentThread().getName() + " outside of mutual exclusive region");
}
}
}
Output:
Thread-0 inside mutual exclusive region
Thread-0 outside of mutual exclusive region
Thread-1 inside mutual exclusive region
Thread-1 outside of mutual exclusive region
Mutex:
· The
word mutex is shorthand for a primitive object that provides MUTual EXclusion
between threads.
· A
mutual exclusion (mutex) is used cooperatively between threads to ensure that
only one of the cooperating threads is allowed to access the data or run
certain application code at a time.
· Any
thread that successfully locks the mutex is the owner until it unlocks the
mutex.
· Any
thread that attempts to lock the mutex waits until the owner unlocks the mutex.
· When
the owner unlocks the mutex, control is returned to one waiting thread with
that thread becoming the owner of the mutex.
· There
can be only one owner of a mutex.
Example
import
com.sun.corba.se.impl.orbutil.concurrent.Mutex;
public class MutexTest {
Mutex
binary = new Mutex();
public static void main(String args[]) {
final MutexTest test = new MutexTest();
new Thread(){
@Override
public void run(){
test.mutualExclusion();
}
}.start();
new Thread(){
@Override
public void run(){
test.mutualExclusion();
}
}.start();
}
private void mutualExclusion() {
try {
binary.acquire();
//mutual exclusive region
System.out.println(Thread.currentThread().getName() + " inside
mutual exclusive region");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
binary.release();
System.out.println(Thread.currentThread().getName() + " outside of
mutual exclusive region");
}
}
}
Output:
Thread-0 inside mutual exclusive region
Thread-0 outside of mutual exclusive region
Thread-1 inside mutual exclusive region
Thread-1 outside of mutual exclusive region
[Multithreading Interview Questions]
We recommend you take Big Data Hadoop class room training at eMexo Technologies in electronic city, Bangalore to learn more about Big Data Hadoop.
0 Comments:
Post a Comment