View Javadoc
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 org.dbunit.util.concurrent;
16  
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  
20  import java.lang.reflect.InvocationTargetException;
21  
22  /**
23   * A one-slot buffer, using semaphores to control access.
24   * Slots are usually more efficient and controllable than using other
25   * bounded buffers implementations with capacity of 1.
26   * <p>
27   * Among other applications, Slots can be convenient in token-passing
28   * designs: Here. the Slot holds a some object serving as a token,
29   * that can be obtained
30   * and returned by various threads.
31   *
32   * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
33   * 
34   * @author Doug Lea
35   * @author Last changed by: $Author$
36   * @version $Revision$ $Date$
37   * @since ? (pre 2.1)
38   */
39  public class Slot extends SemaphoreControlledChannel {
40  
41      /**
42       * Logger for this class
43       */
44      private static final Logger logger = LoggerFactory.getLogger(Slot.class);
45  
46    /**
47     * Create a buffer with the given capacity, using
48     * the supplied Semaphore class for semaphores.
49     * @exception NoSuchMethodException If class does not have constructor 
50     * that intializes permits
51     * @exception SecurityException if constructor information 
52     * not accessible
53     * @exception InstantiationException if semaphore class is abstract
54     * @exception IllegalAccessException if constructor cannot be called
55     * @exception InvocationTargetException if semaphore constructor throws an
56     * exception
57     **/
58  
59    public Slot(Class semaphoreClass) 
60     throws NoSuchMethodException, 
61            SecurityException, 
62            InstantiationException, 
63            IllegalAccessException, 
64            InvocationTargetException {
65      super(1, semaphoreClass);
66    }
67  
68    /** 
69     * Create a new Slot using default Semaphore implementations 
70     **/
71    public Slot() {
72      super(1);
73    }
74  
75    /** The slot **/
76    protected Object item_ = null;
77  
78  
79    /** Set the item in preparation for a take **/
80    protected synchronized void insert(Object x) {
81      item_ = x; 
82    }
83  
84    /** Take item known to exist **/
85    protected synchronized Object extract() {
86          logger.debug("extract() - start");
87   
88      Object x = item_;
89      item_ = null;
90      return x;
91    }
92  
93    public synchronized Object peek() {
94      return item_;
95    }
96  
97  }