com.smartwerkz.jupload.classic.util.concurrent
Class Executor

java.lang.Object
  extended by com.smartwerkz.jupload.classic.util.concurrent.Executor

public class Executor
extends java.lang.Object

Used for executing background tasks. You can add tasks to a queue that the Executor will work through. Also a finalTask can be set that will be executed whenever the Executor finished performing a task and the queue is empty. While executing the final task, the Executor is still busy.
Some critical operations are synchronized against each other to avoid any ambigious results when calling eg start and stop at the same time from different threads.

Since:
Jan 26, 2006, 7:21:34 PM
Author:
Dominik Seifert

Field Summary
protected  Task currentTask
           
protected  ObservedThread thread
           
 
Constructor Summary
Executor(java.lang.String id, java.lang.String name)
          Creates a new Executor that uses the given name.
 
Method Summary
 boolean busy()
          Indicates that the Executor is currently performing a task.
 boolean cancelled()
          Indicates wether this Executor's tasks have just been cancelled.
 boolean cancelTasks()
          Cancels all current tasks and the loading thread will idle as soon as it finished its current task (if it has any).
 Task getCurrentTask()
           
static java.util.List getExecutors(java.lang.String id)
           
 java.lang.String getName()
          Returns the name of the Executor's thread.
 java.lang.Thread getThread()
           
 Validator getValidator()
          Will stop accepting when the current or next set of tasks has been cancelled.
 boolean hasTasks()
          Indicates that there are tasks enqueued that are waiting to be executed.
 boolean isRunning()
          Returns true if the executor has been started with start()
 void performLater(Task task)
          Enqueues a new task to be performed by the Executor.
 void resume()
          Will resume this Executor if suspended.
 void setFinalTask(java.lang.Runnable task)
          Sets the final task which will be executed -if not null- after every time the executor executed all tasks (a task has been executed and the queue is empty) and before it starts waiting for the next.
 void start()
          Starts execution.
 boolean suspend(boolean join)
          Suspends the Executor.
 boolean suspended()
          Indicates wether or not this Executor is suspended.
 void terminate(boolean join)
          Terminates the Executor (call init to restart it).
static void terminateAll(java.lang.String id)
          Terminates all Executors with the given id.
 java.lang.String toString()
           
 boolean waiting()
          Indicates wether the Executor is currently idle and not suspended, thus ready to instantly perform any new task.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

thread

protected ObservedThread thread

currentTask

protected Task currentTask
Constructor Detail

Executor

public Executor(java.lang.String id,
                java.lang.String name)
Creates a new Executor that uses the given name. The ID must be the same for all instances within one jupload context. Thus using the config's id is recommended.

Parameters:
id - the id under which this executor will be stored, unique within one jupload context
name - the name of the executor
Method Detail

start

public void start()
Starts execution.


getName

public java.lang.String getName()
Returns the name of the Executor's thread. Useless if this Executor has been cancelled.


terminate

public void terminate(boolean join)
Terminates the Executor (call init to restart it). If join is true, the calling thread can be ensured that after calling this method, the Executor will be terminated.

Parameters:
join - Indicates that in case, the Executor is currently performing a task, the calling thread should wait for it to finish.

cancelTasks

public boolean cancelTasks()
Cancels all current tasks and the loading thread will idle as soon as it finished its current task (if it has any).

Returns:
Wether this executor is currently busy

performLater

public void performLater(Task task)
Enqueues a new task to be performed by the Executor.


getThread

public java.lang.Thread getThread()

getCurrentTask

public Task getCurrentTask()
Returns:
The task which is currently executed or null.

hasTasks

public boolean hasTasks()
Indicates that there are tasks enqueued that are waiting to be executed. Also with no tasks, the Executor can still be uploading, performing a task.


busy

public boolean busy()
Indicates that the Executor is currently performing a task.


waiting

public boolean waiting()
Indicates wether the Executor is currently idle and not suspended, thus ready to instantly perform any new task.


suspended

public boolean suspended()
Indicates wether or not this Executor is suspended. Also a suspended Executor might still be uploading if it did not finish its last task yet.


suspend

public boolean suspend(boolean join)
Suspends the Executor. If join is true, the calling thread can be ensured that after calling this method, the Executor will be suspended.

Parameters:
join - Indicates that in case, the Executor is currently performing a task, the calling thread should wait for it to finish.
Returns:
Wether it is now idle or -if returning false- still uploading with its last task.

resume

public void resume()
Will resume this Executor if suspended.


setFinalTask

public void setFinalTask(java.lang.Runnable task)
Sets the final task which will be executed -if not null- after every time the executor executed all tasks (a task has been executed and the queue is empty) and before it starts waiting for the next.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getExecutors

public static java.util.List getExecutors(java.lang.String id)
Returns:
A list of Executors with the given id or null if the id does not exist.

terminateAll

public static void terminateAll(java.lang.String id)
Terminates all Executors with the given id.


isRunning

public boolean isRunning()
Returns true if the executor has been started with start()

Returns:
true if the executor has been started, or false if it has been terminated or not yet been started or t

cancelled

public boolean cancelled()
Indicates wether this Executor's tasks have just been cancelled.


getValidator

public Validator getValidator()
Will stop accepting when the current or next set of tasks has been cancelled.



Copyright © 2007 smartwerkz.com. All Rights Reserved.