Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

edu.emory.mathcs.util.concurrent
Class DynamicArrayBlockingQueue  view DynamicArrayBlockingQueue download DynamicArrayBlockingQueue.java

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byedu.emory.mathcs.util.AbstractQueue
          extended byedu.emory.mathcs.util.concurrent.DynamicArrayBlockingQueue
All Implemented Interfaces:
BlockingQueue, java.util.Collection, edu.emory.mathcs.util.Queue

public class DynamicArrayBlockingQueue
extends edu.emory.mathcs.util.AbstractQueue
implements BlockingQueue

This class represents queue of objects. Data is generally stored at the bottom of the queue by put and related operations, and read from the top of the queue by take and related operations. The putAtHead family of operations is also provided to allow storing data at the head of the queue. The underlying implementation uses an array, so no memory allocation occur on queue operations apart from situations where array needs to be resized. The initial and maximum capacity of the queue can be specified at the construction time. The maximum capacity may be also set to "infinity", in which case put operations will never block.

Version:
1.0

Field Summary
(package private)  int beg
           
(package private)  java.lang.Object[] buf
           
(package private)  int end
           
(package private)  int length
           
(package private)  int maxcap
           
 
Constructor Summary
DynamicArrayBlockingQueue()
          Creates new queue object with initial capacity of 16 and unlimited maximum capacity.
DynamicArrayBlockingQueue(int initcap)
          Creates new queue object with specified initial capacity and unlimited maximum capacity.
DynamicArrayBlockingQueue(int initcap, int maxcap)
          Creates new queue object with specified initial capacity and specified maximum capacity.
 
Method Summary
private  boolean enlargeForPut()
           
private  boolean enlargeForPutAtHead()
           
private  boolean full()
           
 java.util.Iterator iterator()
          Obtain an Iterator over this collection.
 boolean offer(java.lang.Object o)
          Puts specified object at the bottom of the queue, if possible.
 boolean offer(java.lang.Object o, long timeout, TimeUnit granularity)
          Puts specified object at the bottom of the queue with specified timeout and returns true if operation succeeded, false otherwise.
 boolean offerAtHead(java.lang.Object o)
          Inserts specified object at the top of the queue, so the next call to get() would return this object.
 boolean offerAtHead(java.lang.Object o, int timeout, TimeUnit granularity)
          Inserts specified object at the top of the queue, so the next call to get() would return this object, with specified timeout and returns true if operation succeeded, false otherwise.
 java.lang.Object peek()
          Return, but do not remove, an element from the queue, or null if the queue is empty.
private  java.lang.Object peek0()
           
 java.lang.Object poll()
          Remove and return an element from the queue if one is available.
 java.lang.Object poll(long timeout, TimeUnit granularity)
          Gets the object from the top of the queue.
 void put(java.lang.Object o)
          Add the given object to the queue, waiting if necessary for space to become available.
private  void put0(java.lang.Object o)
           
 void putAtHead(java.lang.Object o)
          Inserts specified object at the top of the queue, so the next call to poll() or take() would return this object.
private  void putAtHead0(java.lang.Object o)
           
 int remainingCapacity()
          Return the number of elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, or Integer.MAX_VALUE if there is no intrinsic limit.
 int size()
          Returns the number of items in this queue.
 java.lang.Object take()
          Retrieve and remove the first element from the queue, waiting if no objects are present on the queue.
private  java.lang.Object take0()
           
 
Methods inherited from class edu.emory.mathcs.util.AbstractQueue
add, clear, element, remove
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface edu.emory.mathcs.util.Queue
element, remove
 
Methods inherited from interface java.util.Collection
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Field Detail

buf

java.lang.Object[] buf

beg

int beg

end

int end

length

int length

maxcap

int maxcap
Constructor Detail

DynamicArrayBlockingQueue

public DynamicArrayBlockingQueue()
Creates new queue object with initial capacity of 16 and unlimited maximum capacity.


DynamicArrayBlockingQueue

public DynamicArrayBlockingQueue(int initcap)
Creates new queue object with specified initial capacity and unlimited maximum capacity. Initial capacity must be greater than 0, otherwise IllegalArgumentException will be thrown.


DynamicArrayBlockingQueue

public DynamicArrayBlockingQueue(int initcap,
                                 int maxcap)
Creates new queue object with specified initial capacity and specified maximum capacity. Initial capacity must be greater than 0, otherwise IllegalArgumentException will be thrown. Maximum capacity of less than or equal to 0 indicates no limit. Otherwise if the maximum capacity is positive number, it has to be greater than or equal to initial capacity, or IllegalArgumentException will be thrown.

Method Detail

size

public int size()
Returns the number of items in this queue.

Specified by:
size in interface java.util.Collection

remainingCapacity

public int remainingCapacity()
Description copied from interface: BlockingQueue
Return the number of elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, or Integer.MAX_VALUE if there is no intrinsic limit. Note that you cannot always tell if an attempt to add an element will succeed by inspecting remainingCapacity because it may be the case that a waiting consumer is ready to take an element out of an otherwise full queue.

Specified by:
remainingCapacity in interface BlockingQueue

iterator

public java.util.Iterator iterator()
Description copied from interface: java.util.Collection
Obtain an Iterator over this collection.

Specified by:
iterator in interface java.util.Collection

offer

public boolean offer(java.lang.Object o)
Puts specified object at the bottom of the queue, if possible.

Specified by:
offer in interface edu.emory.mathcs.util.Queue

offer

public boolean offer(java.lang.Object o,
                     long timeout,
                     TimeUnit granularity)
              throws java.lang.InterruptedException
Puts specified object at the bottom of the queue with specified timeout and returns true if operation succeeded, false otherwise. If the length of the queue is equal to its maximum capacity, this method will block until either the length decreases (in which case it proceeds with put and returns true) or timeout expires (in which case it returns false), whichever comes first. If the timeout is a positive number, it indicates a number of milliseconds to wait for available space. If the timeout is equal to 0 and the queue is full, the method will immediately return false. If the timeout is negative, the method will never timeout.

Specified by:
offer in interface BlockingQueue

put

public void put(java.lang.Object o)
         throws java.lang.InterruptedException
Description copied from interface: BlockingQueue
Add the given object to the queue, waiting if necessary for space to become available.

Specified by:
put in interface BlockingQueue

offerAtHead

public boolean offerAtHead(java.lang.Object o)
Inserts specified object at the top of the queue, so the next call to get() would return this object.


putAtHead

public void putAtHead(java.lang.Object o)
               throws java.lang.InterruptedException
Inserts specified object at the top of the queue, so the next call to poll() or take() would return this object. If the length of the queue is equal to its maximum capacity, this method will block until the length decreases so that the operation can proceed.


offerAtHead

public boolean offerAtHead(java.lang.Object o,
                           int timeout,
                           TimeUnit granularity)
                    throws java.lang.InterruptedException
Inserts specified object at the top of the queue, so the next call to get() would return this object, with specified timeout and returns true if operation succeeded, false otherwise. If the length of the queue is equal to its maximum capacity, this method will block until either the length decreases (in which case it proceeds with insert and returns true) or timeout expires (in which case it returns false), whichever comes first. If the timeout is a positive number, it indicates a number of milliseconds to wait for available space. If the timeout is equal to 0 and the queue is full, the method will immediately return false. If the timeout is negative, the method will never timeout.


take

public java.lang.Object take()
                      throws java.lang.InterruptedException
Retrieve and remove the first element from the queue, waiting if no objects are present on the queue.

Specified by:
take in interface BlockingQueue

poll

public java.lang.Object poll(long timeout,
                             TimeUnit granularity)
                      throws java.lang.InterruptedException
Gets the object from the top of the queue. If the queue is empty, this method will block until either the length increases (in which case it proceeds with get) or timeout expires (in which case it returns null), whichever comes first. If the timeout is a positive number, it indicates a number of milliseconds to wait. If the timeout is equal to 0 and the queue is empty, the method will immediately return null. If the timeout is negative, the method will never timeout.

Specified by:
poll in interface BlockingQueue

peek

public java.lang.Object peek()
Description copied from interface: edu.emory.mathcs.util.Queue
Return, but do not remove, an element from the queue, or null if the queue is empty. This method returns the same object reference that would be returned by by the poll method. The two methods differ in that this method does not remove the element from the queue.

Specified by:
peek in interface edu.emory.mathcs.util.Queue

poll

public java.lang.Object poll()
Remove and return an element from the queue if one is available.

Specified by:
poll in interface edu.emory.mathcs.util.Queue

take0

private java.lang.Object take0()

peek0

private java.lang.Object peek0()

put0

private void put0(java.lang.Object o)

putAtHead0

private void putAtHead0(java.lang.Object o)

enlargeForPut

private boolean enlargeForPut()

enlargeForPutAtHead

private boolean enlargeForPutAtHead()

full

private boolean full()