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

Quick Search    Search Deep

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

java.lang.Object
  extended byedu.emory.mathcs.util.concurrent.ThreadFactoryUser
      extended byedu.emory.mathcs.util.concurrent.QueuedExecutor
All Implemented Interfaces:
Executor

public class QueuedExecutor
extends ThreadFactoryUser
implements Executor

An implementation of Executor that queues incoming requests until they can be processed by a single background thread.

The thread is not actually started until the first execute request is encountered. Also, if the thread is stopped for any reason (for example, after hitting an unrecoverable exception in an executing task), one is started upon encountering a new request, or if restart() is invoked.

Beware that, especially in situations where command objects themselves invoke execute, queuing can sometimes lead to lockups, since commands that might allow other threads to terminate do not run at all when they are in the queue.

[ Introduction to this package. ]


Nested Class Summary
protected  class QueuedExecutor.RunLoop
          The runloop is isolated in its own Runnable class just so that the main class need not implement Runnable, which would allow others to directly invoke run, which would never make sense here.
 
Field Summary
protected static java.lang.Runnable ENDTASK
          Special queue element to signal termination
protected  int finalCountdown
           
private  int pending
           
protected  BlockingQueue queue_
          The queue
protected  QueuedExecutor.RunLoop runLoop_
           
protected  boolean shutdown_
          true if thread should shut down after processing current task
protected  java.lang.Thread thread_
          The thread used to process commands
protected  long timeout
          how long to wait before stopping runner for empty queue
 
Fields inherited from class edu.emory.mathcs.util.concurrent.ThreadFactoryUser
threadFactory_
 
Constructor Summary
QueuedExecutor()
          Construct a new QueuedExecutor that uses a BoundedLinkedQueue with the current DefaultChannelCapacity as its queue.
QueuedExecutor(BlockingQueue queue)
          Construct a new QueuedExecutor that uses the supplied Channel as its queue.
QueuedExecutor(BlockingQueue queue, long timeout)
          Construct a new QueuedExecutor that uses the supplied Channel as its queue, and with specified timeout
QueuedExecutor(long timeout)
          Construct a new QueuedExecutor that uses a BoundedLinkedQueue with the current DefaultChannelCapacity as its queue, and with specified timeout
 
Method Summary
private  void decreasePending()
           
 void execute(java.lang.Runnable command)
          Arrange for execution of the command in the background thread by adding it to the queue.
private  java.lang.Runnable getNextTask()
           
private  int getPending()
           
 java.lang.Thread getThread()
          Return the thread being used to process commands, or null if there is no such thread.
private  void increasePending()
           
 void shutdownAfterProcessingCurrentlyQueuedTasks()
          Terminate background thread after it processes all elements currently in queue.
 void shutdownAfterProcessingCurrentTask()
          Terminate background thread after it processes the current task, removing other queued tasks and leaving them unprocessed.
 void shutdownNow()
          Terminate background thread even if it is currently processing a task.
private  void stop()
          Called from within the run loop when the processing thread is about to exit.
 
Methods inherited from class edu.emory.mathcs.util.concurrent.ThreadFactoryUser
getThreadFactory, setThreadFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

thread_

protected java.lang.Thread thread_
The thread used to process commands


ENDTASK

protected static java.lang.Runnable ENDTASK
Special queue element to signal termination


shutdown_

protected volatile boolean shutdown_
true if thread should shut down after processing current task


finalCountdown

protected int finalCountdown

timeout

protected long timeout
how long to wait before stopping runner for empty queue


queue_

protected final BlockingQueue queue_
The queue


runLoop_

protected final QueuedExecutor.RunLoop runLoop_

pending

private int pending
Constructor Detail

QueuedExecutor

public QueuedExecutor(BlockingQueue queue,
                      long timeout)
Construct a new QueuedExecutor that uses the supplied Channel as its queue, and with specified timeout

This class does not support any methods that reveal this queue. If you need to access it independently (for example to invoke any special status monitoring operations), you should record a reference to it separately.


QueuedExecutor

public QueuedExecutor(BlockingQueue queue)
Construct a new QueuedExecutor that uses the supplied Channel as its queue.

This class does not support any methods that reveal this queue. If you need to access it independently (for example to invoke any special status monitoring operations), you should record a reference to it separately.


QueuedExecutor

public QueuedExecutor(long timeout)
Construct a new QueuedExecutor that uses a BoundedLinkedQueue with the current DefaultChannelCapacity as its queue, and with specified timeout


QueuedExecutor

public QueuedExecutor()
Construct a new QueuedExecutor that uses a BoundedLinkedQueue with the current DefaultChannelCapacity as its queue.

Method Detail

getThread

public java.lang.Thread getThread()
Return the thread being used to process commands, or null if there is no such thread. You can use this to invoke any special methods on the thread, for example, to interrupt it.


getNextTask

private java.lang.Runnable getNextTask()
                                throws java.lang.InterruptedException

increasePending

private void increasePending()

getPending

private int getPending()

decreasePending

private void decreasePending()

stop

private void stop()
Called from within the run loop when the processing thread is about to exit. This method may cause spawning a new processing thread to replace the exiting one if there are pending execution requests.


execute

public void execute(java.lang.Runnable command)
Arrange for execution of the command in the background thread by adding it to the queue. The method may block if the channel's put operation blocks.

If the background thread does not exist, it is created and started. DK: made synchronized to avoid the following race condition: execute() invokes restart() to make sure thread is alive, then runloop discovers that queue is empty and destroys the thread, then execute calls queue.put() with the command that will never be executed since thread is killed. Note: it is not correct to reverse the order of queue.put() and restart(), since queue may be implemented without buffer (blocking until put and get meet).

Specified by:
execute in interface Executor

shutdownAfterProcessingCurrentlyQueuedTasks

public void shutdownAfterProcessingCurrentlyQueuedTasks()
Terminate background thread after it processes all elements currently in queue. Any tasks entered after this point will not be processed. A shut down thread cannot be restarted. This method may block if the task queue is finite and full. Also, this method does not in general apply (and may lead to comparator-based exceptions) if the task queue is a priority queue.


shutdownAfterProcessingCurrentTask

public void shutdownAfterProcessingCurrentTask()
Terminate background thread after it processes the current task, removing other queued tasks and leaving them unprocessed. A shut down thread cannot be restarted.


shutdownNow

public void shutdownNow()
Terminate background thread even if it is currently processing a task. This method uses Thread.interrupt, so relies on tasks themselves responding appropriately to interruption. If the current tasks does not terminate on interruption, then the thread will not terminate until processing current task. A shut down thread cannot be restarted.