Slot.java

  1. /*
  2.   File: Slot.java

  3.   Originally written by Doug Lea and released into the public domain.
  4.   This may be used for any purposes whatsoever without acknowledgment.
  5.   Thanks for the assistance and support of Sun Microsystems Labs,
  6.   and everyone contributing, testing, and using this code.

  7.   History:
  8.   Date       Who                What
  9.   11Jun1998  dl               Create public version
  10.   25aug1998  dl               added peek
  11. */

  12. package org.dbunit.util.concurrent;

  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;

  15. import java.lang.reflect.InvocationTargetException;

  16. /**
  17.  * A one-slot buffer, using semaphores to control access.
  18.  * Slots are usually more efficient and controllable than using other
  19.  * bounded buffers implementations with capacity of 1.
  20.  * <p>
  21.  * Among other applications, Slots can be convenient in token-passing
  22.  * designs: Here. the Slot holds a some object serving as a token,
  23.  * that can be obtained
  24.  * and returned by various threads.
  25.  *
  26.  * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
  27.  *
  28.  * @author Doug Lea
  29.  * @author Last changed by: $Author$
  30.  * @version $Revision$ $Date$
  31.  * @since ? (pre 2.1)
  32.  */
  33. public class Slot extends SemaphoreControlledChannel {

  34.     /**
  35.      * Logger for this class
  36.      */
  37.     private static final Logger logger = LoggerFactory.getLogger(Slot.class);

  38.   /**
  39.    * Create a buffer with the given capacity, using
  40.    * the supplied Semaphore class for semaphores.
  41.    * @exception NoSuchMethodException If class does not have constructor
  42.    * that intializes permits
  43.    * @exception SecurityException if constructor information
  44.    * not accessible
  45.    * @exception InstantiationException if semaphore class is abstract
  46.    * @exception IllegalAccessException if constructor cannot be called
  47.    * @exception InvocationTargetException if semaphore constructor throws an
  48.    * exception
  49.    **/

  50.   public Slot(Class semaphoreClass)
  51.    throws NoSuchMethodException,
  52.           SecurityException,
  53.           InstantiationException,
  54.           IllegalAccessException,
  55.           InvocationTargetException {
  56.     super(1, semaphoreClass);
  57.   }

  58.   /**
  59.    * Create a new Slot using default Semaphore implementations
  60.    **/
  61.   public Slot() {
  62.     super(1);
  63.   }

  64.   /** The slot **/
  65.   protected Object item_ = null;


  66.   /** Set the item in preparation for a take **/
  67.   protected synchronized void insert(Object x) {
  68.     item_ = x;
  69.   }

  70.   /** Take item known to exist **/
  71.   protected synchronized Object extract() {
  72.         logger.debug("extract() - start");
  73.  
  74.     Object x = item_;
  75.     item_ = null;
  76.     return x;
  77.   }

  78.   public synchronized Object peek() {
  79.     return item_;
  80.   }

  81. }