Class DynamicTask<T,P,R>
java.lang.Object
com.pranavpandey.android.dynamic.util.concurrent.DynamicRunnable<T,P,R>
com.pranavpandey.android.dynamic.util.concurrent.DynamicTask<T,P,R>
- Type Parameters:
T
- The type of the params.P
- The type of the progress.R
- The type of the result.
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
ContextTask
,FileDeleteTask
Base class to represent
Runnable
according to the DynamicRunnable
.-
Constructor Summary
ConstructorDescriptionConstructor to initialize an object of this class.DynamicTask
(Looper looper) Constructor to initialize an object of this class. -
Method Summary
Modifier and TypeMethodDescriptionfinal boolean
cancel
(boolean mayInterruptIfRunning) Attempts to cancel execution of this task.final DynamicTask<T,
P, R> execute()
Executes the task with the default executor andnull
parameters.final DynamicTask<T,
P, R> Executes the task with the supplied parameters.final DynamicTask<T,
P, R> executeOnExecutor
(Executor executor) Executes the task on the supplied executor.final DynamicTask<T,
P, R> executeOnExecutor
(Executor executor, T params) Executes the task on the supplied executor.void
finish
(DynamicResult<R> result) This method will be called to handle the result returned by the task.final DynamicResult<R>
get()
Waits if necessary for the computation to complete, and then retrieves its result.final DynamicResult<R>
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.boolean
getBooleanResult
(DynamicResult<R> result) Returns the unboxedboolean
value from theBoolean
result.Returns the thread handler used by this task.final DynamicStatus
Returns the current status of this task.final boolean
Returnstrue
if this task was cancelled before it completed normally.publishProgress
(DynamicResult<P> progress) This method can be invoked fromDynamicRunnable.doInBackground(T)
to publish updates on the UI thread while the background computation is still running.void
run()
Methods inherited from class com.pranavpandey.android.dynamic.util.concurrent.DynamicRunnable
doInBackground, onCancelled, onCancelled, onPostExecute, onPreExecute, onProgressUpdate
-
Constructor Details
-
DynamicTask
public DynamicTask()Constructor to initialize an object of this class. -
DynamicTask
Constructor to initialize an object of this class.- Parameters:
looper
- The looper to be used.
-
-
Method Details
-
getHandler
Returns the thread handler used by this task.- Returns:
- The thread handler used by this task.
-
getStatus
Returns the current status of this task.- Returns:
- The current status of this task.
-
get
Waits if necessary for the computation to complete, and then retrieves its result.- Returns:
- The computed result.
- Throws:
CancellationException
- If the computation was cancelled.ExecutionException
- If the computation threw an exception.InterruptedException
- If the current thread was interrupted while waiting.
-
get
@Nullable public final DynamicResult<R> get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.- Parameters:
timeout
- Time to wait before cancelling the operation.unit
- The time unit for the timeout.- Returns:
- The computed result.
- Throws:
CancellationException
- If the computation was cancelled.ExecutionException
- If the computation threw an exception.InterruptedException
- If the current thread was interrupted while waiting.TimeoutException
- If the wait timed out.
-
run
public void run() -
isCancelled
public final boolean isCancelled()Description copied from class:DynamicRunnable
Returnstrue
if this task was cancelled before it completed normally. If you are callingDynamicRunnable.cancel(boolean)
on the task, the value returned by this method should be checked periodically fromDynamicRunnable.doInBackground(Object)
to end the task as soon as possible.- Specified by:
isCancelled
in classDynamicRunnable<T,
P, R> - Returns:
true
if task was cancelled before it completed- See Also:
-
cancel
public final boolean cancel(boolean mayInterruptIfRunning) Description copied from class:DynamicRunnable
Attempts to cancel execution of this task. This attempt will fail if the task has already completed, already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started whencancel
is called, this task should never run. If the task has already started, then themayInterruptIfRunning
parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.Calling this method will result in
DynamicRunnable.onCancelled(DynamicResult)
being invoked on the UI thread afterDynamicRunnable.doInBackground(Object)
returns. Calling this method guarantees that onPostExecute(Object) is never subsequently invoked, even ifcancel
returns false, butDynamicRunnable.onPostExecute(com.pranavpandey.android.dynamic.util.concurrent.DynamicResult<R>)
has not yet run. To finish the task as early as possible, checkDynamicRunnable.isCancelled()
periodically fromDynamicRunnable.doInBackground(Object)
.This only requests cancellation. It never waits for a running background task to terminate, even if
mayInterruptIfRunning
is true.- Specified by:
cancel
in classDynamicRunnable<T,
P, R> - Parameters:
mayInterruptIfRunning
-true
if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete.- Returns:
false
if the task could not be cancelled, typically because it has already completed normally;true
otherwise- See Also:
-
executeOnExecutor
@MainThread @NonNull public final DynamicTask<T,P, executeOnExecutorR> (@NonNull Executor executor, @Nullable T params) Executes the task on the supplied executor.- Parameters:
executor
- The executor to execute the task.params
- The optional parameters for the task.- Returns:
- The instance of the executed task.
-
executeOnExecutor
Executes the task on the supplied executor.- Parameters:
executor
- The executor to execute the task.- Returns:
- The instance of the executed task.
-
execute
Executes the task with the supplied parameters.- Parameters:
params
- The parameters for the task.- Returns:
- The instance of the executed task.
- See Also:
-
execute
Executes the task with the default executor andnull
parameters.- Returns:
- The instance of the executed task.
- See Also:
-