KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > multithreating > Semaphore


1 package com.knowgate.multithreating;
2
3 //-< Semaphore.java >------------------------------------------------*--------*
4
// JSYNC Version 1.04 (c) 1998 GARRET * ? *
5
// (Java synchronization classes) * /\| *
6
// * / \ *
7
// Created: 20-Jun-98 K.A. Knizhnik * / [] \ *
8
// Last update: 10-Jul-98 K.A. Knizhnik * GARRET *
9
// http://www.garret.ru/~knizhnik/java.html *
10
//-------------------------------------------------------------------*--------*
11
// Simple semaphore with wait() signal() operations
12
//-------------------------------------------------------------------*--------*
13

14
15 /** Classical Dijkstra semaphore with <code>wait()</code> and
16  * <code>signal()</code> operations.
17  * @author Konstantin Knizhnik
18  * @version 1.04
19  */

20
21 public final class Semaphore {
22     /** Wait for non-zero value of counter.
23      */

24     public synchronized void waitSemaphore()
25       throws InterruptedException JavaDoc {
26         while (counter == 0) {
27             try {
28                 wait();
29             } catch(InterruptedException JavaDoc ex) {
30                 // It is possible for a thread to be interrupted after
31
// being notified but before returning from the wait()
32
// call. To prevent lost of notification notify()
33
// is invoked.
34
notify();
35                 throw new InterruptedException JavaDoc("Thread was interrupted");
36             }
37         }
38         counter -= 1;
39     }
40
41     /** Wait at most <code>timeout</code> miliseconds for non-zero value
42      * of counter.
43      *
44      * @param timeout the maximum time to wait in milliseconds.
45      * @return <code>true</code> if counter is not zero, <code>false</code>
46      * if <code>wait()</code> was terminated due to timeout expiration.
47      */

48     public synchronized boolean waitSemaphore(long timeout)
49       throws InterruptedException JavaDoc {
50         if (counter == 0) {
51             long startTime = System.currentTimeMillis();
52             do {
53                 long currentTime = System.currentTimeMillis();
54                 if (currentTime - startTime >= timeout) {
55                     return false;
56                 }
57                 try {
58                     wait(timeout - currentTime + startTime);
59                 } catch(InterruptedException JavaDoc ex) {
60                     // It is possible for a thread to be interrupted after
61
// being notified but before returning from the wait()
62
// call. To prevent lost of notification notify()
63
// is invoked.
64
notify();
65                     throw new InterruptedException JavaDoc("Thread was interrupted");
66                 }
67             } while (counter == 0);
68         }
69         counter -= 1;
70         return true;
71     }
72
73     /** Increment value of the counter. If there are waiting threads, exactly
74      * one of them will be awaken.
75      */

76     public synchronized void signal() {
77         counter += 1;
78         notify();
79     }
80
81     /** Create semaphore with zero counter value.
82      */

83     public Semaphore() { counter = 0; }
84
85     /** Create semaphore with specified non-negative counter value.
86      *
87      * @param initValue initial value of semaphore counter
88      */

89     public Semaphore(int initValue) {
90         counter = initValue;
91     }
92
93     protected int counter;
94 }
95
96
97
Popular Tags