Filter:   InfoImg
download SemaphoreControlledChannel.java
Language: Java
LOC: 94
Project Info
Lyophilizer
Server: SourceForge
Type: cvs
...wego\cs\dl\util\concurrent\
   Barrier.java
   BoundedBuffer.java
   BoundedChannel.java
   BoundedLinkedQueue.java
   BoundedPriorityQueue.java
   ...enBarrierException.java
   Callable.java
   Channel.java
   ClockDaemon.java
   ConcurrentHashMap.java
   ...rrentReaderHashMap.java
   CondVar.java
   CopyOnWriteArrayList.java
   CopyOnWriteArraySet.java
   CountDown.java
   CyclicBarrier.java
   ...ultChannelCapacity.java
   DirectExecutor.java
   Executor.java
   FIFOReadWriteLock.java
   FIFOSemaphore.java
   FJTask.java
   FJTaskRunner.java
   FJTaskRunnerGroup.java
   FutureResult.java
   Heap.java
   Latch.java
   LayeredSync.java
   LinkedNode.java
   LinkedQueue.java
   LockedExecutor.java
   Mutex.java
   NullSync.java
   ObservableSync.java
   PooledExecutor.java
   PrioritySemaphore.java
   ...yChangeMulticaster.java
   Puttable.java
   QueuedExecutor.java
   QueuedSemaphore.java
   ...renceReadWriteLock.java
   ReadWriteLock.java
   ReentrantLock.java
   ...renceReadWriteLock.java
   Rendezvous.java
   Semaphore.java
   ...eControlledChannel.java
   Slot.java
   Sync.java
   SyncCollection.java
   SynchronizedBoolean.java
   SynchronizedByte.java
   SynchronizedChar.java
   SynchronizedDouble.java
   SynchronizedFloat.java
   SynchronizedInt.java
   SynchronizedLong.java
   SynchronizedRef.java
   SynchronizedShort.java
   SynchronizedVariable.java
   SynchronousChannel.java
   SyncList.java
   SyncMap.java
   SyncSet.java
   SyncSortedMap.java
   SyncSortedSet.java
   Takable.java
   ThreadedExecutor.java
   ThreadFactory.java
   ThreadFactoryUser.java
   TimeDaemon.java
   TimedCallable.java
   TimeoutException.java
   TimeoutSync.java
   ...eChangeMulticaster.java
   WaitableBoolean.java
   WaitableByte.java
   WaitableChar.java
   WaitableDouble.java
   WaitableFloat.java
   WaitableInt.java
   WaitableLong.java
   WaitableRef.java
   WaitableShort.java
   ...referenceSemaphore.java
   WaitFreeQueue.java
   ...renceReadWriteLock.java

/*
  File: SemaphoreControlledChannel.java

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

  History:
  Date       Who                What
  16Jun1998  dl               Create public version
   5Aug1998  dl               replaced int counters with longs
*/

package EDU.oswego.cs.dl.util.concurrent;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Constructor;

/**
 * Abstract class for channels that use Semaphores to
 * control puts and takes.
 * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
 **/

public abstract class SemaphoreControlledChannel implements BoundedChannel {
  protected final Semaphore putGuard_;
  protected final Semaphore takeGuard_;
  protected int capacity_;

  /**
   * Create a channel with the given capacity and default
   * semaphore implementation
   * @exception IllegalArgumentException if capacity less or equal to zero
   **/

  public SemaphoreControlledChannel(int capacity) 
   throws IllegalArgumentException {
    if (capacity <= 0) throw new IllegalArgumentException();
    capacity_ = capacity;
    putGuard_ = new Semaphore(capacity);
    takeGuard_ = new Semaphore(0);
  }


  /**
   * Create a channel with the given capacity and 
   * semaphore implementations instantiated from the supplied class
   * @exception IllegalArgumentException if capacity less or equal to zero.
   * @exception NoSuchMethodException If class does not have constructor 
   * that intializes permits
   * @exception SecurityException if constructor information 
   * not accessible
   * @exception InstantiationException if semaphore class is abstract
   * @exception IllegalAccessException if constructor cannot be called
   * @exception InvocationTargetException if semaphore constructor throws an
   * exception
   **/
  public SemaphoreControlledChannel(int capacity, Class semaphoreClass) 
   throws IllegalArgumentException, 
          NoSuchMethodException, 
          SecurityException, 
          InstantiationException, 
          IllegalAccessException, 
          InvocationTargetException {
    if (capacity <= 0) throw new IllegalArgumentException();
    capacity_ = capacity;
    Class[] intarg = { Integer.TYPE };
    Constructor ctor = semaphoreClass.getDeclaredConstructor(intarg);
    Integer[] cap = { new Integer(capacity) };
    putGuard_ = (Semaphore)(ctor.newInstance(cap));
    Integer[] zero = { new Integer(0) };
    takeGuard_ = (Semaphore)(ctor.newInstance(zero));
  }



  public int  capacity() { return capacity_; }

  /** 
   * Return the number of elements in the buffer.
   * This is only a snapshot value, that may change
   * immediately after returning.
   **/

  public int size() { return (int)(takeGuard_.permits());  }

  /**
   * Internal mechanics of put.
   **/
  protected abstract void insert(Object x);

  /**
   * Internal mechanics of take.
   **/
  protected abstract Object extract();

  public void put(Object x) throws InterruptedException {
    if (x == null) throw new IllegalArgumentException();
    if (Thread.interrupted()) throw new InterruptedException();
    putGuard_.acquire();
    try {
      insert(x);
      takeGuard_.release();
    }
    catch (ClassCastException ex) {
      putGuard_.release();
      throw ex;
    }
  }

  public boolean offer(Object x, long msecs) throws InterruptedException {
    if (x == null) throw new IllegalArgumentException();
    if (Thread.interrupted()) throw new InterruptedException();
    if (!putGuard_.attempt(msecs)) 
      return false;
    else {
      try {
        insert(x);
        takeGuard_.release();
        return true;
      }
      catch (ClassCastException ex) {
        putGuard_.release();
        throw ex;
      }
    }
  }

  public Object take() throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    takeGuard_.acquire();
    try {
      Object x = extract();
      putGuard_.release();
      return x;
    }
    catch (ClassCastException ex) {
      takeGuard_.release();
      throw ex;
    }
  }

  public Object poll(long msecs) throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    if (!takeGuard_.attempt(msecs))
      return null;
    else {
      try {
        Object x = extract();
        putGuard_.release();
        return x;
      }
      catch (ClassCastException ex) {
        takeGuard_.release();
        throw ex;
      }
    }
  }

}