BoundedBuffer.java

  1. /*
  2.   File: BoundedBuffer.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.   17Jul1998  dl               Simplified by eliminating wait counts
  11.   25aug1998  dl               added peek
  12.    5May1999  dl               replace % with conditional (slightly faster)
  13. */

  14. package org.dbunit.util.concurrent;

  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;

  17. /**
  18.  * Efficient array-based bounded buffer class.
  19.  * Adapted from CPJ, chapter 8, which describes design.
  20.  * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>] <p>
  21.  *
  22.  * @author Doug Lea
  23.  * @author Last changed by: $Author$
  24.  * @version $Revision$ $Date$
  25.  * @since ? (pre 2.1)
  26.  */
  27. public class BoundedBuffer implements BoundedChannel {

  28.     /**
  29.      * Logger for this class
  30.      */
  31.     private static final Logger logger = LoggerFactory.getLogger(BoundedBuffer.class);

  32.   protected final Object[]  array_;      // the elements

  33.   protected int takePtr_ = 0;            // circular indices
  34.   protected int putPtr_ = 0;      

  35.   protected int usedSlots_ = 0;          // length
  36.   protected int emptySlots_;             // capacity - length

  37.   /**
  38.    * Helper monitor to handle puts.
  39.    **/
  40.   protected final Object putMonitor_ = new Object();

  41.   /**
  42.    * Create a BoundedBuffer with the given capacity.
  43.    * @exception IllegalArgumentException if capacity less or equal to zero
  44.    **/
  45.   public BoundedBuffer(int capacity) throws IllegalArgumentException {
  46.     if (capacity <= 0) throw new IllegalArgumentException();
  47.     array_ = new Object[capacity];
  48.     emptySlots_ = capacity;
  49.   }

  50.   /**
  51.    * Create a buffer with the current default capacity
  52.    **/

  53.   public BoundedBuffer() {
  54.     this(DefaultChannelCapacity.get());
  55.   }

  56.   /**
  57.    * Return the number of elements in the buffer.
  58.    * This is only a snapshot value, that may change
  59.    * immediately after returning.
  60.    **/
  61.   public synchronized int size() {
  62.  return usedSlots_;
  63.  }

  64.   public int capacity() {
  65.     return array_.length;
  66.  }

  67.   protected void incEmptySlots() {
  68.     synchronized(putMonitor_) {
  69.       ++emptySlots_;
  70.       putMonitor_.notify();
  71.     }
  72.   }

  73.   protected synchronized void incUsedSlots() {
  74.     ++usedSlots_;
  75.     notify();
  76.   }

  77.   protected final void insert(Object x) {
  78.         logger.debug("insert(x={}) - start", x);
  79.  // mechanics of put
  80.     --emptySlots_;
  81.     array_[putPtr_] = x;
  82.     if (++putPtr_ >= array_.length) putPtr_ = 0;
  83.   }

  84.   protected final Object extract() {
  85.         logger.debug("extract() - start");
  86.  // mechanics of take
  87.     --usedSlots_;
  88.     Object old = array_[takePtr_];
  89.     array_[takePtr_] = null;
  90.     if (++takePtr_ >= array_.length) takePtr_ = 0;
  91.     return old;
  92.   }

  93.   public Object peek() {
  94.         logger.debug("peek() - start");

  95.     synchronized(this) {
  96.       if (usedSlots_ > 0)
  97.         return array_[takePtr_];
  98.       else
  99.         return null;
  100.     }
  101.   }


  102.   public void put(Object x) throws InterruptedException {
  103.         logger.debug("put(x={}) - start", x);

  104.     if (x == null) throw new IllegalArgumentException();
  105.     if (Thread.interrupted()) throw new InterruptedException();

  106.     synchronized(putMonitor_) {
  107.       while (emptySlots_ <= 0) {
  108.     try { putMonitor_.wait(); }
  109.         catch (InterruptedException ex) {
  110.           putMonitor_.notify();
  111.           throw ex;
  112.         }
  113.       }
  114.       insert(x);
  115.     }
  116.     incUsedSlots();
  117.   }

  118.   public boolean offer(Object x, long msecs) throws InterruptedException {
  119.         logger.debug("offer(x={}, msecs={}) - start", x, msecs);

  120.     if (x == null) throw new IllegalArgumentException();
  121.     if (Thread.interrupted()) throw new InterruptedException();

  122.     synchronized(putMonitor_) {
  123.       long start = (msecs <= 0)? 0 : System.currentTimeMillis();
  124.       long waitTime = msecs;
  125.       while (emptySlots_ <= 0) {
  126.         if (waitTime <= 0) return false;
  127.     try { putMonitor_.wait(waitTime); }
  128.         catch (InterruptedException ex) {
  129.           putMonitor_.notify();
  130.           throw ex;
  131.         }
  132.         waitTime = msecs - (System.currentTimeMillis() - start);
  133.       }
  134.       insert(x);
  135.     }
  136.     incUsedSlots();
  137.     return true;
  138.   }



  139.   public  Object take() throws InterruptedException {
  140.         logger.debug("take() - start");
  141.  
  142.     if (Thread.interrupted()) throw new InterruptedException();
  143.     Object old = null;
  144.     synchronized(this) {
  145.       while (usedSlots_ <= 0) {
  146.         try { wait(); }
  147.         catch (InterruptedException ex) {
  148.           notify();
  149.           throw ex;
  150.         }
  151.       }
  152.       old = extract();
  153.     }
  154.     incEmptySlots();
  155.     return old;
  156.   }

  157.   public  Object poll(long msecs) throws InterruptedException {
  158.         logger.debug("poll(msecs={}) - start", msecs);
  159.  
  160.     if (Thread.interrupted()) throw new InterruptedException();
  161.     Object old = null;
  162.     synchronized(this) {
  163.       long start = (msecs <= 0)? 0 : System.currentTimeMillis();
  164.       long waitTime = msecs;
  165.      
  166.       while (usedSlots_ <= 0) {
  167.         if (waitTime <= 0) return null;
  168.         try { wait(waitTime); }
  169.         catch (InterruptedException ex) {
  170.           notify();
  171.           throw ex;
  172.         }
  173.         waitTime = msecs - (System.currentTimeMillis() - start);

  174.       }
  175.       old = extract();
  176.     }
  177.     incEmptySlots();
  178.     return old;
  179.   }

  180. }