public class AsyncTask extends Task
An AsyncTask is used to execute a single method in a target Object, or a static method in a class in a separate thread, returning a Handle that can be used to monitor and (in some cases) control the execution of the method.
This can be a convenient way of doing a background task without having to create a separate anonymous Thread or Task for that particular task, which would increase the size of your code.
This class is most useful for particular method which take a Handle as a parameter and which use the provided Handle to determine if the execution of the method should abort. This gives another Thread the ability to abort the execution by calling the stop() method on the Handle. Examples of this are eve.io.File.listFiles(Handle h, String mask, int options) and eve.data.TreeNode.expand(Handle h).
With methods like these the AsyncTask will automatically create a Handle, send it to the method in the background Thread and return it to the foreground Thread. The foreground thread can then stop the method by calling stop() on it if needed.
Note: the current implementation of AsyncTask is a Task, which is itself a Handle. So in fact creating the new thread involves the calling of doRun() on this AsyncTask which then passes itself as the handle to the target Method and so the Handle returned by the various invoke() methods is this same AsyncTask. However future implementations may do this differently so you should not assume that the returned Handle is this same AsyncTask.
Note Also: When the method is complete and has returned the return value of the target method (if any) is placed in the returnValue field of the Handle and the Handle flags Success and Stopped will be set. The only time the Failed flag will be set is if the method returns a boolean type and it returns false, OR if the the stop() method is called on the Handle, OR if an exception is thrown (in which case the exception is placed in the error field of the Handle).
Handle.ObjectDiscarder, Handle.WaitOnChange
Aborted, Changed, DEFAULT_CHANGE_ACCEPT_TIME, DEFAULT_PROGRESS_RESOLUTION, doing, error, Failed, Failure, OPTION_STOP_IF_NOT_SET, OPTION_TIMEOUT_IF_NOT_SET, progress, progressResolution, returnCode, returnValue, Running, shouldStop, startTime, state, Stopped, stopReason, subHandle, Succeeded, Success, waitChangeAcceptedTime
Constructor and Description |
---|
AsyncTask()
Create a new AsyncTask.
|
AsyncTask(Class objectClass,
String nameAndParametersAndType,
boolean declaredOnly)
Create a new AsyncTask specifying the class of the target object and the full name
and specs for the method.
|
AsyncTask(String objectClass,
String nameAndParametersAndType)
Create a new AsyncTask specifying the class of the target object as a String and the full name
and specs for the method.
|
Modifier and Type | Method and Description |
---|---|
protected void |
doRun()
Override this to provide functionality for the Task.
|
Handle |
invoke(Object target)
Invoke the method on the target in a separate Thread and return a Handle to the task.
|
Handle |
invoke(Object target,
Object[] parameters)
Invoke the method on the target in a separate Thread and return a Handle to the task.
|
Handle |
invoke(Object target,
Object[] parameters,
int handleIndex)
Invoke the method on the target in a separate Thread and return a Handle to the task.
|
Handle |
invoke(Object targetOrClass,
String nameAndParametersAndType)
This method combines a call to setMethod() followed by a call to invoke().
|
Handle |
invoke(Object targetOrClass,
String nameAndParametersAndType,
Object[] parameters)
This method combines a call to setMethod() followed by a call to invoke().
|
Handle |
invoke(Object targetOrClass,
String nameAndParametersAndType,
Object[] parameters,
int handleIndex)
This method combines a call to setMethod() followed by a call to invoke().
|
static Handle |
invokeOn(Object targetOrClass,
String nameAndParametersAndType,
Object[] parameters)
This method invokes the method in the background returning a Handle used to monitor
the running task.
|
AsyncTask |
setMethod(Class objectClass,
String nameAndParametersAndType,
boolean declaredOnly)
Set the method given the class of the target object and the full name
and specs for the method.
|
AsyncTask |
setMethod(Method execMethod)
Set the Method to be executed by the AsyncTask.
|
complete, getDaemon, getThread, interrupt, run, setDaemon, setThreadGroup, sleep, start, start
addParent, changeAccepted, changed, check, check, checkAbortFail, checkAny, checkChangeAccepted, checkFailure, convertError, doChangeTo, fail, getChangeState, getErrorText, getFinalReturnValue, getProperties, getReturnValue, getStoppableWaitOnSuccess, getUnwrappedReturnValue, hasStopped, newWaitOnChange, removeParent, resetProgress, set, setFlags, setProgress, setProgress, setResult, setReturnValue, startDoing, stop, succeed, succeeded, throwErrorIfNotStopped, throwRuntimeError, throwRuntimeError, throwRuntimeError, throwRuntimeError, timeout, timeout, waitChangeAccepted, waitForReturnValue, waitOn, waitOn, waitOn, waitOn, waitOnAny, waitOnAny, waitOnAny, waitOnAny, waitOnChange, waitOnResult, waitOnSuccess, waitOnSuccess, waitReturnValue, waitUntilCompletion, waitUntilCompletion, waitUntilStopped, waitUntilStopped, wasAborted, yield
public AsyncTask()
public AsyncTask(Class objectClass, String nameAndParametersAndType, boolean declaredOnly)
objectClass
- the Class of the target object.nameAndParametersAndType
- the fully encoded name and type of the method: e.g.
invokeMethod(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;Z)Leve/sys/Wrapper;declaredOnly
- true to search only for methods actually declared by the class and
not by any superclasses.public AsyncTask(String objectClass, String nameAndParametersAndType)
objectClass
- the normal dot encoded Java name of the class (e.g. "java.lang.Object").nameAndParametersAndType
- the fully encoded name and type of the method: e.g.
invokeMethod(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;Z)Leve/sys/Wrapper;public AsyncTask setMethod(Method execMethod)
execMethod
- the Method to be executed by the AsyncTask.public AsyncTask setMethod(Class objectClass, String nameAndParametersAndType, boolean declaredOnly)
objectClass
- the Class of the target object.nameAndParametersAndType
- the fully encoded name and type of the method: e.g.
invokeMethod(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;Z)Leve/sys/Wrapper;declaredOnly
- true to search only for methods actually declared by the class and
not by any superclasses.public Handle invoke(Object target, Object[] parameters, int handleIndex)
target
- the target object.parameters
- the parameters to be sent to the method. Leave the parameter which
is supposed to be a provided Handle as null and a new Handle will be created and
provided.handleIndex
- the index of the Handle parameter. This AsyncTask will be sent
as the Handle parameter. If handleIndex is -1 then no Handle is sent. If you want
the handleIndex to be determined automatically use the invoke(Object target, Object[] parameters) method
instead.public Handle invoke(Object target, Object[] parameters)
target
- the target object.parameters
- the parameters to be sent to the method. Leave the parameter which
is supposed to be a provided Handle as null and a new Handle will be created and
provided.handleIndex
- the index of the Handle parameter. This AsyncTask will be sent
as the Handle parameter. If handleIndex is -1 then no Handle is sent. If you want
the handleIndex to be determined automatically use the invoke(Object target, Object[] parameters) method
instead.public Handle invoke(Object target)
target
- the target object.public Handle invoke(Object targetOrClass, String nameAndParametersAndType)
targetOrClass
- The target object or a Class object if the target method
is static. This method can be used if the method takes no parameters OR if the
method takes only one parameter which is a Handle. In this case, the Handle will provided.nameAndParametersAndType
- the fully encoded name and type of the method: e.g.
invokeMethod(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;Z)Leve/sys/Wrapper;public Handle invoke(Object targetOrClass, String nameAndParametersAndType, Object[] parameters, int handleIndex)
targetOrClass
- The target object or a Class object if the target method
is static.nameAndParametersAndType
- the fully encoded name and type of the method: e.g.
invokeMethod(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;Z)Leve/sys/Wrapper;parameters
- the parameters to be sent to the method. Leave the parameter which
is supposed to be a provided Handle as null and a new Handle will be created and
provided.handleIndex
- the index of the Handle parameter. This AsyncTask will be sent
as the Handle parameter. If handleIndex is -1 then no Handle is sent. If you want
the handleIndex to be determined automatically use the method that does not have a
handleIndex parameter.public Handle invoke(Object targetOrClass, String nameAndParametersAndType, Object[] parameters)
targetOrClass
- The target object or a Class object if the target method
is static.nameAndParametersAndType
- the fully encoded name and type of the method: e.g.
invokeMethod(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;Z)Leve/sys/Wrapper;parameters
- the parameters to be sent to the method. Leave the parameter which
is supposed to be a provided Handle as null and a new Handle will be created and
provided.public static Handle invokeOn(Object targetOrClass, String nameAndParametersAndType, Object[] parameters)
targetOrClass
- The target object or a Class object if the target method
is static.nameAndParametersAndType
- the fully encoded name and type of the method: e.g.
invokeMethod(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;Z)Leve/sys/Wrapper;parameters
- the parameters to be sent to the method. Leave the parameter which
is supposed to be a provided Handle as null and a new Handle will be created and
provided.