Code - Class EDU.oswego.cs.dl.util.concurrent.Slot


1 /*
2   File: Slot.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   11Jun1998 dl Create public version
12   25aug1998 dl added peek
13 */

14
15 package EDU.oswego.cs.dl.util.concurrent;
16 import java.lang.reflect.*;
17
18 /**
19  * A one-slot buffer, using semaphores to control access.
20  * Slots are usually more efficient and controllable than using other
21  * bounded buffers implementations with capacity of 1.
22  * <p>
23  * Among other applications, Slots can be convenient in token-passing
24  * designs: Here. the Slot holds a some object serving as a token,
25  * that can be obtained
26  * and returned by various threads.
27  *
28  * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
29 **/

30
31 public class Slot extends SemaphoreControlledChannel {
32
33   /**
34    * Create a buffer with the given capacity, using
35    * the supplied Semaphore class for semaphores.
36    * @exception NoSuchMethodException If class does not have constructor
37    * that intializes permits
38    * @exception SecurityException if constructor information
39    * not accessible
40    * @exception InstantiationException if semaphore class is abstract
41    * @exception IllegalAccessException if constructor cannot be called
42    * @exception InvocationTargetException if semaphore constructor throws an
43    * exception
44    **/

45
46   public Slot(Class semaphoreClass)
47    throws NoSuchMethodException,
48           SecurityException,
49           InstantiationException,
50           IllegalAccessException,
51           InvocationTargetException {
52     super(1, semaphoreClass);
53   }
54
55   /**
56    * Create a new Slot using default Semaphore implementations
57    **/

58   public Slot() {
59     super(1);
60   }
61
62   /** The slot **/
63   protected Object item_ = null;
64
65
66   /** Set the item in preparation for a take **/
67   protected synchronized void insert(Object x) {
68     item_ = x;
69   }
70
71   /** Take item known to exist **/
72   protected synchronized Object extract() {
73     Object x = item_;
74     item_ = null;
75     return x;
76   }
77
78   public synchronized Object peek() {
79     return item_;
80   }
81
82 }
83

Java API By Example, From Geeks To Geeks. | Conditions of Use | About Us © 2002 - 2005, KickJava.com, or its affiliates