Here is a simple Semaphore implementation:
public class Semaphore {
private boolean signal = false;
public synchronized void take() {
this.signal = true;
this.notify();
}
public synchronized void release() throws InterruptedException{
while(!this.signal) wait();
this.signal = false;
}
}
The take()
method sends a signal which is stored internally in the Semaphore. The release()
method waits for a signal. When received the signal flag is cleared again, and the release()
method exited.
Read this article and take a look at this example