Jumpi v1.2.0

org.jumpi.impl.util
Class BoundedQueue

java.lang.Object
  |
  +--org.jumpi.impl.util.BoundedQueue

public class BoundedQueue
extends java.lang.Object

Efficient array-based bounded buffer class. Adapted from CPJ, chapter 8, which describes design.

[ Introduction to this package. ]


Constructor Summary
BoundedQueue(int capacity)
          Create a BoundedBuffer with the given capacity.
 
Method Summary
 int capacity()
          The capacity of the queue.
 void clear()
           Clears the queue.
 java.lang.Object peek()
          Return the first element in the queue without blocking.
 java.lang.Object poll(long msecs)
          Poll for up to msecs milliseconds for an element to become available for taking.
 void put(java.lang.Object x)
          Put an element at the end of the queue.
 boolean put(java.lang.Object x, long msecs)
          Put an element at the back of the queue, waiting at most msecs milliseconds to do so if the queue is full.
 int size()
          Return the number of elements in the buffer.
 java.lang.Object take()
          Get the first element in the queue, blocking indefinitely until there is one available.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BoundedQueue

public BoundedQueue(int capacity)
             throws java.lang.IllegalArgumentException
Create a BoundedBuffer with the given capacity.

Parameters:
capacity - the maximum number of elements in the queue.
Throws:
java.lang.IllegalArgumentException - if capacity less or equal to zero
Method Detail

size

public int size()
Return the number of elements in the buffer. This is only a snapshot value, that may change immediately after returning.

Returns:
the number of elements in the queue.

capacity

public int capacity()
The capacity of the queue.

Returns:
the invariant capacity of the queue.

peek

public java.lang.Object peek()
Return the first element in the queue without blocking. If the queue is empty then null is returned.

Returns:
the first element in the queue, or null if empty.

put

public void put(java.lang.Object x)
Put an element at the end of the queue. Block indefinitely until there is space in the queue to place the element.

Parameters:
x - the element to insert.
Throws:
java.lang.IllegalArgumentException - when x is null.

put

public boolean put(java.lang.Object x,
                   long msecs)
Put an element at the back of the queue, waiting at most msecs milliseconds to do so if the queue is full.

Parameters:
x - the element to insert.
msecs - the maximum time interval to wait if the queue is full.
Returns:
true if the insertion took place, otherwise false and the queue is unchanged.
Throws:
java.lang.IllegalArgumentException - if x is null.

take

public java.lang.Object take()
Get the first element in the queue, blocking indefinitely until there is one available.

Returns:
the first element in the queue.

poll

public java.lang.Object poll(long msecs)
Poll for up to msecs milliseconds for an element to become available for taking. If msecs is negative or zero, then no waiting is performed.

Parameters:
msecs - maximum time to wait in milliseconds.
Returns:
next object in the queue, or null if none are available after msecs interval.

clear

public void clear()

Clears the queue.

Any threads which were blocking at the capacity limit to put elements into the queue can proceed.


Jumpi v1.2.0

Copyright 2003, Peter Jonathan Klauser.