miércoles, 1 de abril de 2015

Semaphores in java

Please, see this tutorial

http://tutorials.jenkov.com/java-concurrency/semaphores.html

Example of Semaphore as a lock:

public class Semaphore {

private static final Logger LOG = LoggerFactory.getLogger(Semaphore.class);
private boolean signal = true; // It means that the first threads could get the light green

/**

*/
public Semaphore() {

super();
}

/**

*/
public synchronized void release() {

this.signal = true;
this.notifyAll();
}

/**

*/
public synchronized void take() {

while(this.signal == false) {
try {
wait();
} catch (InterruptedException e) {
LOG.error(e.getMessage(), e);
}
}
this.signal = false;
}
}