Class QueuedExecutor
- All Implemented Interfaces:
Executor
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected class
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.Nested classes/interfaces inherited from class EDU.oswego.cs.dl.util.concurrent.ThreadFactoryUser
ThreadFactoryUser.DefaultThreadFactory
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static Runnable
Special queue element to signal terminationprotected final Channel
The queueprotected final QueuedExecutor.RunLoop
protected boolean
true if thread should shut down after processing current taskprotected Thread
The thread used to process commandsFields inherited from class EDU.oswego.cs.dl.util.concurrent.ThreadFactoryUser
threadFactory_
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new QueuedExecutor that uses a BoundedLinkedQueue with the current DefaultChannelCapacity as its queue.QueuedExecutor
(Channel queue) Construct a new QueuedExecutor that uses the supplied Channel as its queue. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
set thread_ to null to indicate terminationvoid
Arrange for execution of the command in the background thread by adding it to the queue.Return the thread being used to process commands, or null if there is no such thread.void
restart()
Start (or restart) the background thread to process commands.void
Terminate background thread after it processes all elements currently in queue.void
Terminate background thread after it processes the current task, removing other queued tasks and leaving them unprocessed.void
Terminate background thread even if it is currently processing a task.Methods inherited from class EDU.oswego.cs.dl.util.concurrent.ThreadFactoryUser
getThreadFactory, setThreadFactory
-
Field Details
-
thread_
The thread used to process commands -
ENDTASK
Special queue element to signal termination -
shutdown_
protected volatile boolean shutdown_true if thread should shut down after processing current task -
queue_
The queue -
runLoop_
-
-
Constructor Details
-
QueuedExecutor
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()Construct a new QueuedExecutor that uses a BoundedLinkedQueue with the current DefaultChannelCapacity as its queue.
-
-
Method Details
-
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. -
clearThread
protected void clearThread()set thread_ to null to indicate termination -
restart
public void restart()Start (or restart) the background thread to process commands. It has no effect if a thread is already running. This method can be invoked if the background thread crashed due to an unrecoverable exception. -
execute
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.
- Specified by:
execute
in interfaceExecutor
- Throws:
InterruptedException
-
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.
-