home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 1.0 KB | 50 lines |
- // Semaphore.java
- // 10.02.96
- //
- // implements a semaphore for synchronization
-
- package cybcerone.utils;
-
- /**
- * Your basic semaphore, used for synchronization.
- * Only one object can hold the semaphore at any given point in time.
- */
- public class Semaphore {
- private boolean occupied;
- private Object owner;
-
- public Semaphore () {
- occupied = false;
- owner = null;
- }
-
- /**
- * Request the semaphore, if it's not free wait until it is.
- *
- * @param requester The object requesting the semaphore
- */
- public synchronized void p (Object requester) {
- while (occupied) {
- try {
- wait ();
- } catch (InterruptedException e) {
- }
- }
- occupied = true;
- owner = requester;
- }
-
- /**
- * Release the semaphore. No effect if releaser isn't the current owner.
- *
- * @param releaser The object releasing the semaphore.
- */
- public synchronized void v (Object releaser) {
- if (releaser == owner) {
- occupied = false;
- owner = null;
- notifyAll ();
- }
- }
- }
-