DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Java Concurrency – Part 4 : Semaphores

Java Concurrency – Part 4 : Semaphores

Baptiste Wicht user avatar by
Baptiste Wicht
·
Sep. 08, 10 · Java Zone · Interview
Like (0)
Save
Tweet
27.88K Views

Join the DZone community and get the full member experience.

Join For Free

We continue with the Java Concurrency theme with semaphores. Semaphores are also a way to synchronize threads.

Semaphores are a really simple concept, invented by the famous Dutch computer scientist Edsger Dijkstra. Basically a semaphore is a counter (integer) that allows a thread to get into a critical region if the value of the counter is greater than 0. If it’s the case, the counter is decremented by one. Otherwise, the thread waits until it can go. And when the thread leaves the critical region, the counter is incremented by one to allow one more thread to pass through the critical region. A semaphore is created with a certain value for its counter. So, you can execute two actions on a semaphore P and V.

For example, if you have a critical section that cannot be executed concurrently, you can use a semaphore :

sem mutex = new sem(1)

P(mutex)
//Critical region
V(mutex)

So you must always call the P operation by yourself before the critical region and V after it. We call a mutex (mutual exclusion) a semaphore with a value of one. So only one thread can enter the region guarded by the semaphore. This is the most used semaphore. The other use of semaphore is to guard a set of resources like database connections or a data pool.

In Java, a semaphore is created using the java.util.concurrent.Semaphore class. You can create it easily :

Semaphore mutex = new Semaphore(1);
Semaphore available = new Semaphore(100);

The P and V operations are represented using the acquire and release methods. The method acquire can be interrupted if the thread is interrupted. There is an ininterruptible version with the method acquireUninterruptibly(). There is also a third version with the tryAcquire method. This method acquires a permit only if there is one permit available, otherwise, this method return false. All the waiting methods also have an overloaded version with a timeout. You can also acquire several permits at once using the permits argument to the different versions of acquire methods.

A little example with a mutex using the same example as the previous post on Java concurrency :

public class Example {
private int value = 0;

private final Semaphore mutex = new Semaphore(1)

public int getNextValue() throws InterruptedException {
try {
mutex.acquire();
return value++;
} finally {
mutex.release();
}
}
}

For more information about Semaphore in Java, the best is to consult the Javadoc of the Semaphore class.

To conclude, semaphores are a powerful way to solve concurrency problems, but this is not adapted to all problems. If you only need mutual exclusion, synchronized blocks are a better solution. The problems with semaphores is that you can forget to call the release method and this can cause deadlocks that are sometimes difficult to find.

From http://www.baptiste-wicht.com/2010/08/java-concurrency-part-4-semaphores/

Semaphore (programming) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why Is Software Integration Important for Business?
  • Maven Tutorial: Nice and Easy [Video]
  • Memory Debugging and Watch Annotations
  • A Simple Guide to Heaps, Stacks, References, and Values in JavaScript

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo