KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > util > Semaphore


1 package org.jacorb.trading.util;
2
3 /**
4  * Simple, straight forward implementation of a binary semaphore.
5  *
6  * @author Nicolas Noffke
7  */

8 public class Semaphore {
9     private int count;
10     
11     /**
12      * Constructor. Sets this Semaphore up as a binary one.
13      *
14      */

15     public Semaphore() {
16     count = 1;
17     }
18
19     /**
20      * Constructor. Sets the initial value of this Semaphore
21      * to start_value
22      *
23      */

24     public Semaphore(int start_value){
25     count = start_value;
26     }
27
28     /**
29      * P-Operation. Blocks until somebody else calls V().
30      *
31      */

32     public synchronized void P() {
33     while (count == 0){
34         try{
35         wait();
36         } catch (InterruptedException JavaDoc e){
37         }
38     }
39     count = 0;
40     }
41  
42     /**
43      * V-Operation, unblocks this semaphore
44      *
45      */

46     public synchronized void V() {
47     count = 1;
48     notifyAll();
49     }
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Popular Tags