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

Quick Search    Search Deep

org.ematgine.utils.concurrent
Class SynchronousChannel  view SynchronousChannel download SynchronousChannel.java

java.lang.Object
  extended byorg.ematgine.utils.concurrent.SynchronousChannel
All Implemented Interfaces:
BoundedChannel, Channel, Puttable, Takable

public class SynchronousChannel
extends java.lang.Object
implements BoundedChannel

[ Introduction to this package. ]

A rendezvous channel, similar to those used in CSP and Ada. Each put must wait for a take, and vice versa.

Synchronous channels are well suited for handoff designs, in which an object running in one thread must synch up with an object running in another thread in order to hand it some information, event, or task. This implementation uses WaiterPreferenceSemaphores to help avoid infinite overtaking among multiple threads trying to perform puts or takes.

If you only need threads to synch up without exchanging information, consider using a Barrier. If you need bidirectional exchanges, consider using a Rendezvous.

For a usage example, see the implementation of PooledExecutor

[ Introduction to this package. ]


Field Summary
protected  java.lang.Object item_
          The item currently being transferred.
protected  Semaphore itemAvailable_
          (binary) Semaphore that is released when an item has been put, so a take may proceed.
protected  Semaphore itemTaken_
          (binary) Semaphore that is released when an item has been taken, so a put may proceed.
protected  java.lang.Object onePut_
          Lock to ensure that only one put at a time proceeds
protected  Semaphore unclaimedTakers_
          Semaphore maintaining count of number of takes that are waiting for puts.
 
Constructor Summary
SynchronousChannel()
          Create a new channel.
 
Method Summary
 int capacity()
          Return the maximum number of elements that can be held.
protected  void doPut(java.lang.Object x)
           
protected  java.lang.Object doTake(boolean timed, long msecs)
           
protected  java.lang.Object extract()
          Take item known to exist
protected  void insert(java.lang.Object x)
          Set the item in preparation for a take
 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.
 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
 

Field Detail

item_

protected java.lang.Object item_
The item currently being transferred.


unclaimedTakers_

protected Semaphore unclaimedTakers_
Semaphore maintaining count of number of takes that are waiting for puts.


itemAvailable_

protected Semaphore itemAvailable_
(binary) Semaphore that is released when an item has been put, so a take may proceed.


itemTaken_

protected Semaphore itemTaken_
(binary) Semaphore that is released when an item has been taken, so a put may proceed. Since only one thread at a time can ever be waiting on this, fairness is not important, so we use cheapest implementation.


onePut_

protected java.lang.Object onePut_
Lock to ensure that only one put at a time proceeds

Constructor Detail

SynchronousChannel

public SynchronousChannel()
Create a new channel.

Method Detail

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

insert

protected void insert(java.lang.Object x)
Set the item in preparation for a take


extract

protected java.lang.Object extract()
Take item known to exist


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

take

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

Specified by:
take in interface Channel

poll

public java.lang.Object poll(long msecs)
                      throws java.lang.InterruptedException
Description copied from interface: Channel
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 Channel

doTake

protected java.lang.Object doTake(boolean timed,
                                  long msecs)
                           throws java.lang.InterruptedException

put

public void put(java.lang.Object x)
         throws java.lang.InterruptedException
Description copied from interface: Channel
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 Channel

doPut

protected void doPut(java.lang.Object x)

offer

public boolean offer(java.lang.Object x,
                     long msecs)
              throws java.lang.InterruptedException
Description copied from interface: Channel
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 Channel