org.metasyntactic.thread.concurrent
Class BoundedBuffer

java.lang.Object
  |
  +--org.metasyntactic.thread.concurrent.BoundedBuffer
All Implemented Interfaces:
BoundedChannel, Channel, Puttable, Takable

public class BoundedBuffer
extends java.lang.Object
implements BoundedChannel

Efficient array-based bounded buffer class.


Constructor Summary
BoundedBuffer()
          Create a buffer with the current default capacity
BoundedBuffer(int capacity)
          Create a BoundedBuffer with the given capacity.
 
Method Summary
 int capacity()
          Return the maximum number of elements that can be held.
 boolean offer(java.lang.Object x, long msecs)
          Place item in channel only if it can be accepted within msecs milliseconds.
 java.lang.Object peek()
          Return, but do not remove object at head of Channel, or null if it is empty.
 java.lang.Object poll(long msecs)
          Return and remove an item from channel only if one is available within msecs milliseconds.
 void put(java.lang.Object x)
          Place item in the channel, possibly waiting indefinitely until it can be accepted.
 int size()
          Return the number of elements in the buffer.
 java.lang.Object take()
          Return and remove an item from channel, possibly waiting indefinitely until such an item exists.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BoundedBuffer

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

Parameters:
capacity - The capacity of the buffer (not modifiable)
Throws:
java.lang.IllegalArgumentException - if capacity less or equal to zero

BoundedBuffer

public BoundedBuffer()
Create a buffer with the current default capacity

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 buffer

capacity

public int capacity()
Description copied from interface: BoundedChannel
Return the maximum number of elements that can be held.

Specified by:
capacity in interface BoundedChannel
Returns:
the capacity of this channel.

peek

public java.lang.Object peek()
Description copied from interface: Channel
Return, but do not remove object at head of Channel, or null if it is empty.

Specified by:
peek in interface Channel
Returns:
The object (possibly null) @ the head of the channel

put

public void put(java.lang.Object x)
         throws java.lang.InterruptedException
Description copied from interface: Puttable
Place item in the channel, possibly waiting indefinitely until it can be accepted. Channels implementing the BoundedChannel subinterface are generally guaranteed to block on puts upon reaching capacity, but other implementations may or may not block.

Specified by:
put in interface Puttable
Parameters:
x - the element to be inserted. Should be non-null.
Throws:
java.lang.InterruptedException - if the current thread has been interrupted at a point at which interruption is detected, in which case the element is guaranteed not to be inserted. Otherwise, on normal return, the element is guaranteed to have been inserted.

offer

public boolean offer(java.lang.Object x,
                     long msecs)
              throws java.lang.InterruptedException
Description copied from interface: Puttable
Place item in channel only if it can be accepted within msecs milliseconds. The time bound is interpreted in a coarse-grained, best-effort fashion.

Specified by:
offer in interface Puttable
Parameters:
x - the element to be inserted. Should be non-null.
msecs - the number of milliseconds to wait. If less than or equal to zero, the method does not perform any timed waits, but might still require access to a synchronization lock, which can impose unbounded delay if there is a lot of contention for the channel.
Returns:
true if accepted, else false
Throws:
java.lang.InterruptedException - if the current thread has been interrupted at a point at which interruption is detected, in which case the element is guaranteed not to be inserted (i.e., is equivalent to a false return).

take

public java.lang.Object take()
                      throws java.lang.InterruptedException
Description copied from interface: Takable
Return and remove an item from channel, possibly waiting indefinitely until such an item exists.

Specified by:
take in interface Takable
Returns:
some item from the channel. Different implementations may guarantee various properties (such as FIFO) about that item
Throws:
java.lang.InterruptedException - if the current thread has been interrupted at a point at which interruption is detected, in which case state of the channel is unchanged.

poll

public java.lang.Object poll(long msecs)
                      throws java.lang.InterruptedException
Description copied from interface: Takable
Return and remove an item from channel only if one is available within msecs milliseconds. The time bound is interpreted in a coarse grained, best-effort fashion.

Specified by:
poll in interface Takable
Parameters:
msecs - the number of milliseconds to wait. If less than or equal to zero, the operation does not perform any timed waits, but might still require access to a synchronization lock, which can impose unbounded delay if there is a lot of contention for the channel.
Returns:
some item, or null if the channel is empty.
Throws:
java.lang.InterruptedException - if the current thread has been interrupted at a point at which interruption is detected, in which case state of the channel is unchanged (i.e., equivalent to a false return).