org.jjoost.util.concurrent
Class ThreadQueue<Q extends ThreadQueue<Q>>

java.lang.Object
  extended by org.jjoost.util.concurrent.ThreadQueue<Q>

public class ThreadQueue<Q extends ThreadQueue<Q>>
extends java.lang.Object

A simple thread queue used by concurrent collections implementations to track threads that are waiting on conditions being met (e.g. in the case of the LockFreeHashStore, a hash node being deleted or a bucket being migrated) This class is intended as a base class to be extended to contain state against the waiting thread, indicating in some way the resource it is waiting on. The standard use is to have a final "head" instance never containing a waiting thread, on which insert() is called, by the thread that is to be parked, with a new link (constructed with the calling thread as argument) to be added to the end of the chain. Once the insert() method returns a loop checking the state of the resource the thread is waiting on should be entered, within which (if this check fails) the thread should be put to sleep using LockSupport.park(). Once the loop's condition is met, the thread should call the remove() method on the link it inserted; e.g.

 ThreadQueue waitLink = new ThreadQueue(Thead.currentThread()) ;
 waitingOn.insert(waitLink) ; // waitingOn is head of queue 
 while ( {resource is locked test} ) { 
     LockSupport.park() ; 
 } 
 waitLink.remove() ;

The wake(Filter) method provided is intended to act upon this information to wake threads waiting on a now (possibly) free resource. For efficiency it is recommended that a custom wake({condition}) method is written, as the construction/use of a Filter will inherently impede performance. For a relatively small number of waiting threads this implementation performs well, however to accommodate ultra high parallelism it may be worth revisiting with optimisations in the future.

Author:
b.elliottsmith

Constructor Summary
ThreadQueue()
          Construct a new ThreadQueue item with the current thread as argument (this should not be used for construction of the "head" of the queue); new ThreadQueue(null) is the correct constructor.
ThreadQueue(java.lang.Thread thread)
          Construct a new ThreadQueue
 
Method Summary
 void insert(Q insert)
          Insert a new link to the end of the chain this link is a member of
 void remove()
          Remove this link from the chain
 void wake(Filter<Q> wake)
          wake up all links after this link on which application of the provided filter's accept() method returns true
 void wakeAll()
          Wakes up all threads in this queue
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadQueue

public ThreadQueue()
Construct a new ThreadQueue item with the current thread as argument (this should not be used for construction of the "head" of the queue); new ThreadQueue(null) is the correct constructor.


ThreadQueue

public ThreadQueue(java.lang.Thread thread)
Construct a new ThreadQueue

Parameters:
thread - thread that is waiting
Method Detail

wakeAll

public void wakeAll()
Wakes up all threads in this queue


wake

public void wake(Filter<Q> wake)
wake up all links after this link on which application of the provided filter's accept() method returns true

Parameters:
wake - filter indicating which links should be woken

remove

public void remove()
Remove this link from the chain


insert

public void insert(Q insert)
Insert a new link to the end of the chain this link is a member of

Parameters:
insert - the link to be inserted at the end of the chain