KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > util > concurrent > SemaphoreControlledChannel


1 /*
2   File: SemaphoreControlledChannel.java
3
4   Originally written by Doug Lea and released into the public domain.
5   This may be used for any purposes whatsoever without acknowledgment.
6   Thanks for the assistance and support of Sun Microsystems Labs,
7   and everyone contributing, testing, and using this code.
8
9   History:
10   Date Who What
11   16Jun1998 dl Create public version
12    5Aug1998 dl replaced int counters with longs
13   08dec2001 dl reflective constructor now uses longs too.
14 */

15
16 package org.dbunit.util.concurrent;
17 import java.lang.reflect.Constructor JavaDoc;
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19
20 /**
21  * Abstract class for channels that use Semaphores to
22  * control puts and takes.
23  * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
24  **/

25
26 public abstract class SemaphoreControlledChannel implements BoundedChannel {
27   protected final Semaphore putGuard_;
28   protected final Semaphore takeGuard_;
29   protected int capacity_;
30
31   /**
32    * Create a channel with the given capacity and default
33    * semaphore implementation
34    * @exception IllegalArgumentException if capacity less or equal to zero
35    **/

36
37   public SemaphoreControlledChannel(int capacity)
38    throws IllegalArgumentException JavaDoc {
39     if (capacity <= 0) throw new IllegalArgumentException JavaDoc();
40     capacity_ = capacity;
41     putGuard_ = new Semaphore(capacity);
42     takeGuard_ = new Semaphore(0);
43   }
44
45
46   /**
47    * Create a channel with the given capacity and
48    * semaphore implementations instantiated from the supplied class
49    * @exception IllegalArgumentException if capacity less or equal to zero.
50    * @exception NoSuchMethodException If class does not have constructor
51    * that intializes permits
52    * @exception SecurityException if constructor information
53    * not accessible
54    * @exception InstantiationException if semaphore class is abstract
55    * @exception IllegalAccessException if constructor cannot be called
56    * @exception InvocationTargetException if semaphore constructor throws an
57    * exception
58    **/

59   public SemaphoreControlledChannel(int capacity, Class JavaDoc semaphoreClass)
60    throws IllegalArgumentException JavaDoc,
61           NoSuchMethodException JavaDoc,
62           SecurityException JavaDoc,
63           InstantiationException JavaDoc,
64           IllegalAccessException JavaDoc,
65           InvocationTargetException JavaDoc {
66     if (capacity <= 0) throw new IllegalArgumentException JavaDoc();
67     capacity_ = capacity;
68     Class JavaDoc[] longarg = { Long.TYPE };
69     Constructor JavaDoc ctor = semaphoreClass.getDeclaredConstructor(longarg);
70     Long JavaDoc[] cap = { new Long JavaDoc(capacity) };
71     putGuard_ = (Semaphore)(ctor.newInstance(cap));
72     Long JavaDoc[] zero = { new Long JavaDoc(0) };
73     takeGuard_ = (Semaphore)(ctor.newInstance(zero));
74   }
75
76
77
78   public int capacity() { return capacity_; }
79
80   /**
81    * Return the number of elements in the buffer.
82    * This is only a snapshot value, that may change
83    * immediately after returning.
84    **/

85
86   public int size() { return (int)(takeGuard_.permits()); }
87
88   /**
89    * Internal mechanics of put.
90    **/

91   protected abstract void insert(Object JavaDoc x);
92
93   /**
94    * Internal mechanics of take.
95    **/

96   protected abstract Object JavaDoc extract();
97
98   public void put(Object JavaDoc x) throws InterruptedException JavaDoc {
99     if (x == null) throw new IllegalArgumentException JavaDoc();
100     if (Thread.interrupted()) throw new InterruptedException JavaDoc();
101     putGuard_.acquire();
102     try {
103       insert(x);
104       takeGuard_.release();
105     }
106     catch (ClassCastException JavaDoc ex) {
107       putGuard_.release();
108       throw ex;
109     }
110   }
111
112   public boolean offer(Object JavaDoc x, long msecs) throws InterruptedException JavaDoc {
113     if (x == null) throw new IllegalArgumentException JavaDoc();
114     if (Thread.interrupted()) throw new InterruptedException JavaDoc();
115     if (!putGuard_.attempt(msecs))
116       return false;
117     else {
118       try {
119         insert(x);
120         takeGuard_.release();
121         return true;
122       }
123       catch (ClassCastException JavaDoc ex) {
124         putGuard_.release();
125         throw ex;
126       }
127     }
128   }
129
130   public Object JavaDoc take() throws InterruptedException JavaDoc {
131     if (Thread.interrupted()) throw new InterruptedException JavaDoc();
132     takeGuard_.acquire();
133     try {
134       Object JavaDoc x = extract();
135       putGuard_.release();
136       return x;
137     }
138     catch (ClassCastException JavaDoc ex) {
139       takeGuard_.release();
140       throw ex;
141     }
142   }
143
144   public Object JavaDoc poll(long msecs) throws InterruptedException JavaDoc {
145     if (Thread.interrupted()) throw new InterruptedException JavaDoc();
146     if (!takeGuard_.attempt(msecs))
147       return null;
148     else {
149       try {
150         Object JavaDoc x = extract();
151         putGuard_.release();
152         return x;
153       }
154       catch (ClassCastException JavaDoc ex) {
155         takeGuard_.release();
156         throw ex;
157       }
158     }
159   }
160
161 }
162
Popular Tags