|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Thread
live.threads.ThreadedUserAction
This function serves as a base class for all of the classes that will actually do useful work. For each action you want the user to be able to do, subclass this and implement three functions:
runImpl()
should perform the actiondescription()
should return a one-line summary of what is being
doneresult()
should return the, well, result of the operation once
it is finished, or null if it is sitll ongoing.startJoinForever()
,startJoinTimeout(int)
, or
startNoJoin()
. (You could also call the inherited
Thread.start()
or run()
, but in any application that uses
any of the three custom functions it is recommended that the programmer use
only those three to be consistant and even with the naming conventions.)
Because most of what the subclass will be doing will require parameters, you
have to set them in fields before calling Thread.start()
, just like
you do with any other thread.
Finally, for any subclasses that use the ZVMTasks library, call
init(ThreadInitInfo)
before starting the action. (Probably do this
in the constructor.) Then your subclass can simply use the protected field
tasks
to call the method(s). You could go through the process of
making your subclass keep track of the appropriate info, but why do that when
it's done for you? It is okay to pass init(ThreadInitInfo)
a null
parameter, or for either of the pieces of information in
ThreadInitInfo
to be null, but it's expected that this will be the
case only in testing (for instance, the WaitAction
class) and
possibly other exceptional situations.
Nested Class Summary | |
private static class |
ThreadedUserAction.IDFactory
This is a wrapper class for an int; Integer won't
work because you can't change it; See the init function for
a discussion of why this is (or rather, may be) necessary. |
Field Summary | |
private ActiveThreadsInfo |
activeThreadLists
|
private Cache |
cache
|
static int |
CANCELED
|
static int |
COMPLETE
|
protected boolean |
done
|
static int |
ERROR
|
private Exception |
exception
|
static int |
IN_PROGRESS
|
static int |
INDETERMINATE
|
private static ThreadedUserAction.IDFactory |
nextFreeID
|
static int |
PENDING
|
private String |
resultString
|
private int |
status
|
protected ZVMTasks |
tasks
If init(ThreadInitInfo) has been called with a non-null argument
for which ThreadInitInfo.getConnectionInfo() is non-null, this
will reference a ZVMTasks object which will make the connection to the
server with the authentication parameters defined in
init(ThreadInitInfo) 's connectionInfo#getConnectionInfo(). |
private int |
thisThreadsID
|
private Date |
timeEnded
|
private Date |
timeStarted
|
private boolean |
track
|
Fields inherited from class java.lang.Thread |
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
Constructor Summary | |
ThreadedUserAction()
|
Method Summary | |
abstract String |
description()
This method returns a friendly description of the action the object represents. |
protected Cache |
getCache()
Returns the cache |
Exception |
getException()
Returns the exception thrown |
int |
getID()
Returns the ID of the thread |
int |
getStatus()
Returns the status. |
Date |
getTimeFinished()
This method returns the time at which the thread finished its task and died. |
Date |
getTimeStarted()
This method returns the time at which the thread was started. |
void |
init(ThreadInitInfo initInfo)
Sets the object that tracks the threads. |
void |
init(ThreadInitInfo initInfo,
boolean list)
Sets the object that tracks the threads. |
boolean |
isFinished()
Whether the thread is finished. |
abstract Object |
rawResult()
This returns the result of the call in a manner that is defined by the subclass. |
String |
result()
This returns the result in some manner or other. |
void |
run()
This should be not called by hand under most circumstances [OPTIONAL: except for queries]. |
protected abstract void |
runImpl()
This function should be implemented to do whatever task the class was created to do. |
void |
setCanceled()
Sets the status and resultString to canceled. |
void |
setComplete()
Sets status and resultString to Complete. |
void |
setError()
Sets the status and resultString to Error |
void |
setError(Exception e)
Sets the status to error and the resultString to a specified message |
void |
setIndeterminate()
Sets status and resultString to Indeterminate. |
void |
setIndeterminate(String message)
Sets the status of a task to indeterminate and the resultString to a specified message |
void |
setInProgress()
Sets status and resultString to In Progress. |
void |
startJoinForever()
This function begins the action, returning when it completes. |
boolean |
startJoinTimeout(int timeout_ms)
This function begins the action, returning when it completes, unless it takes more than timeout_ms milliseconds, in which case it returns at that point. |
void |
startNoJoin()
This function begins the action, returning immediately. |
String |
toString()
This method returns a friendly description of the action the object represents. |
Methods inherited from class java.lang.Thread |
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, yield |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
private Date timeStarted
private Date timeEnded
private ActiveThreadsInfo activeThreadLists
private Cache cache
protected ZVMTasks tasks
init(ThreadInitInfo)
has been called with a non-null argument
for which ThreadInitInfo.getConnectionInfo()
is non-null, this
will reference a ZVMTasks object which will make the connection to the
server with the authentication parameters defined in
init(ThreadInitInfo)
's connectionInfo#getConnectionInfo().
protected boolean done
private boolean track
private static ThreadedUserAction.IDFactory nextFreeID
private int thisThreadsID
private Exception exception
public static final int PENDING
public static final int IN_PROGRESS
public static final int COMPLETE
public static final int ERROR
public static final int CANCELED
public static final int INDETERMINATE
private int status
private String resultString
Constructor Detail |
public ThreadedUserAction()
Method Detail |
public final void init(ThreadInitInfo initInfo, boolean list)
initInfo
- The object with server login information and thread listslist
- Should this thread be included in the ActiveThreadsInfo listspublic final void init(ThreadInitInfo initInfo)
initInfo
- The object with server login information and thread listspublic Date getTimeStarted()
getTimeFinished()
public Date getTimeFinished()
getTimeStarted()
, technically this is the last time
the thread died.
getTimeStarted()
public String toString()
toString()
method is called
automatically, so the description is prepended with the class name. For
the description without this prefix, such as for displaying the status
page, use the abstract description()
method instead.
description()
public void run()
runImpl()
, but also sets the starting and ending
times. Instead of calling this, you probably want to call
Thread.start()
, which will start a new thread and begin
executing run()
there.
runImpl()
,
Thread.start()
protected abstract void runImpl() throws Exception
run()
. (But you probably shouldn't call
run()
yourself either.)
Exception
run()
public abstract String description()
toString()
method, this is meant to be
used in, for example, the status page and anywhere else where the end
user will see it.
toString()
public String result()
public abstract Object rawResult()
public void startJoinForever()
run()
(which does not start a new
thread, avoiding the oveheard of going to the JVM; the current
implementation), or 2.Thread.start()
then Thread.join()
,
which starts the thread and then joins the calling and action threads
until the latter dies.
(Actually, the two methods are not exactly equivalent, as you can
interrput a thread that's running, causing join to throw an
InterruptedException exception. There is no equivalent to this with #1
above. However, I'm pretty sure this exception should never be raised.)
public boolean startJoinTimeout(int timeout_ms)
public boolean isFinished()
public void startNoJoin()
Thread.start()
.
public int getID()
public void setInProgress()
public void setComplete()
public void setIndeterminate()
public void setIndeterminate(String message)
message
- Message to displaypublic void setError()
public void setError(Exception e)
e
- Exception to get the error message frompublic void setCanceled()
public int getStatus()
protected Cache getCache()
public Exception getException()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |