|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.fu_berlin.ties.util.TaskRunner
public class TaskRunner
Asynchronously executes any number of Runnable
tasks.
Internally manages a pool of worker threads that are invoked to
asychronously execute tasks as requested. The number of worker threads is
updated on demand so every task is executed without delay.
Usually it should be sufficient to invoke tasks through the static
invokeDefault
methods; in most
cases there should be no reason to create instances of this class.
To avoid the creation of unnecessary instances, it is highly recommended
to register your interest (registerInterest()
) in the default task
runner prior to using it and to deregister
(deregisterInterest()
) when you no longer need it.
A good idea is to do this at the begin and end of your main method.
You should register in a finally
block and you must not
forget to deregister, otherwise your program might run forever
(because the worker threads continue waiting for tasks even after all other
threads have terminated).
When creating your own runner, you must finally call
close(int)
to release each task runner you have created, or your
program might run forever.
Field Summary | |
---|---|
static String |
DEFAULT_NAME
The base name of worker threads used by the default instance. |
Constructor Summary | |
---|---|
TaskRunner(String baseThreadName)
Creates a new instance, using the default priority ( Thread.NORM_PRIORITY ) for threads. |
|
TaskRunner(String baseThreadName,
int defaultPrio)
Creates a new instance. |
Method Summary | |
---|---|
void |
close(int errorCount)
Closes this task runner. |
static void |
deregisterInterest()
Deregisters interest to use the default runner. |
protected void |
finalize()
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
String |
getBaseName()
Returns the base name of worker threads. |
int |
getDefaultPriority()
Returns the default priority to use for threads. |
void |
invoke(Runnable task,
String taskName)
Invokes a task to be executed asynchronously. |
void |
invoke(Runnable task,
String taskName,
int priority)
Invokes a task to be executed asynchronously. |
static void |
invokeDefault(Runnable task,
String taskName)
Invokes a task to be executed asynchronously, using the default task runner. |
static void |
invokeDefault(Runnable task,
String taskName,
int priority)
Invokes a task to be executed asynchronously using the default task runner. |
boolean |
isClosed()
Returns whether this task runner has been closed. |
static void |
registerInterest()
Registers interest to use the default runner. |
String |
toString()
Returns a string representation of this object. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final String DEFAULT_NAME
Constructor Detail |
---|
public TaskRunner(String baseThreadName)
Thread.NORM_PRIORITY
) for threads.
baseThreadName
- the base name of worker threads -- the actual
names are formed by appending the next available numberpublic TaskRunner(String baseThreadName, int defaultPrio) throws IllegalArgumentException
baseThreadName
- the base name of worker threads -- the actual
names are formed by appending the next available numberdefaultPrio
- the priority to use for threads;
should be in the range of Thread.MIN_PRIORITY
to
Thread.MAX_PRIORITY
IllegalArgumentException
- if the priority is not in the range
Thread.MIN_PRIORITY
to
Thread.MAX_PRIORITY
Method Detail |
---|
public static void deregisterInterest()
registerInterest()
prior to calling this method.
You should calls this method in a finally
block to ensure
it is always executed.
public static void invokeDefault(Runnable task, String taskName)
Thread.NORM_PRIORITY
).
task
- the Runnable to be executed asynchronouslytaskName
- a name or short description of the new task --
used for debugging purposespublic static void invokeDefault(Runnable task, String taskName, int priority) throws IllegalArgumentException
task
- the Runnable to be executed asynchronouslytaskName
- a name or short description of the new task --
used for debugging purposespriority
- the priority to use for the thread executing the task;
should be in the range of Thread.MIN_PRIORITY
to
Thread.MAX_PRIORITY
IllegalArgumentException
- if the priority is not in the range
Thread.MIN_PRIORITY
to
Thread.MAX_PRIORITY
public static void registerInterest()
deregisterInterest()
some time after calling this method,
otherwise your program will not terminate regularly.
public final void close(int errorCount)
invoke(Runnable, String)
after calling this method.
close
in interface Closeable
errorCount
- the number of errors (exceptions) that occurred during
calls to this instance (0 if none); ignored by this methodprotected final void finalize()
close(int)
to terminate all worker threads if not yet done.
You should not rely on this method but always call close(int)
yourself.
finalize
in class Object
public final String getBaseName()
public final int getDefaultPriority()
public final void invoke(Runnable task, String taskName)
getDefaultPriority()
).
task
- the Runnable to be executed asynchronouslytaskName
- a name or short description of the new task --
used for debugging purposespublic final void invoke(Runnable task, String taskName, int priority) throws IllegalArgumentException
task
- the Runnable to be executed asynchronouslytaskName
- a name or short description of the new task --
used for debugging purposespriority
- the priority to use for the thread executing the task;
should be in the range of Thread.MIN_PRIORITY
to
Thread.MAX_PRIORITY
IllegalArgumentException
- if the priority is not in the range
Thread.MIN_PRIORITY
to
Thread.MAX_PRIORITY
public final boolean isClosed()
true
iff close(int)
has been called on this
objectpublic String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |