KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > Semaphore


1 package org.sapia.ubik.net;
2
3
4 /**
5  * This class implements a simple semaphore.
6  *
7  * @author Yanick Duchesne
8  * <dl>
9  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
10  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
11  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
12  * </dl>
13  */

14 public class Semaphore {
15   public static final int NO_MAX = 0;
16   private int _maxThread = NO_MAX;
17   private int _count = 0;
18   private ThreadFactory _fac = new DefaultThreadFactory();
19
20   /**
21    * Creates a new instance of this class.
22    *
23    * @param maxThread the number of threads this instance will be
24    * allowed to create.
25    */

26   public Semaphore(int maxThread) {
27     _maxThread = maxThread;
28   }
29
30   /**
31    * Creates a new instance of this class.
32    *
33    * @param maxThread the number of threads this instance will be
34    * allowed to create.
35    * @param fac a <code>ThreadFactory</code> instance.
36    */

37   public Semaphore(int maxThread, ThreadFactory fac) {
38     this(maxThread);
39     _fac = fac;
40   }
41
42   /**
43    * Called when a thread has completed its work.
44    */

45   synchronized void release() {
46     _count = (--_count < 0) ? 0 : _count;
47   }
48
49   /**
50    * Gets the number of currently running threads that have been
51    * borrowed from this instance.
52    *
53    * @return the number of currently running threads.
54    */

55   public synchronized int getThreadCount() {
56     return _count;
57   }
58
59   /**
60    * Returns a thread that wraps the given runnable. The returned
61    * thread is not yet started.
62    * <p>
63    * This method uses this semaphore's internal thread factory to create
64    * the thread objects - the factory implementation can be specified
65    * at semaphore construction time.
66    *
67    * @return a <code>Thread</code> instance.
68    */

69   public synchronized Thread JavaDoc acquireFor(Runnable JavaDoc r)
70     throws MaxThreadReachedException {
71     if ((_maxThread > 0) && (_count >= _maxThread)) {
72       throw new MaxThreadReachedException("" + _count);
73     }
74
75     _count++;
76
77     return _fac.newThreadFor(new SemaphoreRunnable(this, r));
78   }
79
80   /*////////////////////////////////////////////////////////////////////
81                              INNER CLASSES
82   ////////////////////////////////////////////////////////////////////*/

83   public static final class SemaphoreRunnable implements Runnable JavaDoc {
84     private Semaphore _sema;
85     private Runnable JavaDoc _toRun;
86
87     private SemaphoreRunnable(Semaphore s, Runnable JavaDoc toRun) {
88       _sema = s;
89       _toRun = toRun;
90     }
91
92     public final void run() {
93       _toRun.run();
94       _sema.release();
95     }
96   }
97
98   static final class DefaultThreadFactory implements ThreadFactory {
99     /**
100      * @see org.sapia.ubik.net.ThreadFactory#newThreadFor(Runnable)
101      */

102     public Thread JavaDoc newThreadFor(Runnable JavaDoc r) {
103       return new Thread JavaDoc(r);
104     }
105   }
106 }
107
Popular Tags