net.tsz.afinal.common
类 AsyncTask<Params,Progress,Result>

java.lang.Object
  继承者 net.tsz.afinal.common.AsyncTask<Params,Progress,Result>
类型参数:
Params -
Progress -
Result -
直接已知子类:
AjaxRequestHandler

public abstract class AsyncTask<Params,Progress,Result>
extends java.lang.Object

拷贝 https://android.googlesource.com/platform/frameworks/base/+/jb-release/ core/java/android/os/AsyncTask.java 修改了线程池属性,让并发线程按顺序执行

作者:
michael

嵌套类摘要
static class AsyncTask.Status
          Indicates the current status of the task.
 
字段摘要
static java.util.concurrent.Executor DUAL_THREAD_EXECUTOR
           
static java.util.concurrent.Executor SERIAL_EXECUTOR
          An Executor that executes tasks one at a time in serial order.
static java.util.concurrent.Executor THREAD_POOL_EXECUTOR
          An Executor that can be used to execute tasks in parallel.
 
构造方法摘要
AsyncTask()
          Creates a new asynchronous task.
 
方法摘要
 boolean cancel(boolean mayInterruptIfRunning)
          Attempts to cancel execution of this task.
 AsyncTask<Params,Progress,Result> execute(Params... params)
          Executes the task with the specified parameters.
static void execute(java.lang.Runnable runnable)
          Convenience version of execute(Object...) for use with a simple Runnable object.
 AsyncTask<Params,Progress,Result> executeOnExecutor(java.util.concurrent.Executor exec, Params... params)
          Executes the task with the specified parameters.
 Result get()
          Waits if necessary for the computation to complete, and then retrieves its result.
 Result get(long timeout, java.util.concurrent.TimeUnit unit)
          Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.
 AsyncTask.Status getStatus()
          Returns the current status of this task.
static void init()
           
 boolean isCancelled()
          Returns true if this task was cancelled before it completed normally.
static void setDefaultExecutor(java.util.concurrent.Executor exec)
           
 
从类 java.lang.Object 继承的方法
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

THREAD_POOL_EXECUTOR

public static final java.util.concurrent.Executor THREAD_POOL_EXECUTOR
An Executor that can be used to execute tasks in parallel.


SERIAL_EXECUTOR

public static final java.util.concurrent.Executor SERIAL_EXECUTOR
An Executor that executes tasks one at a time in serial order. This serialization is global to a particular process.


DUAL_THREAD_EXECUTOR

public static final java.util.concurrent.Executor DUAL_THREAD_EXECUTOR
构造方法详细信息

AsyncTask

public AsyncTask()
Creates a new asynchronous task. This constructor must be invoked on the UI thread.

方法详细信息

init

public static void init()

setDefaultExecutor

public static void setDefaultExecutor(java.util.concurrent.Executor exec)

getStatus

public final AsyncTask.Status getStatus()
Returns the current status of this task.

返回:
The current status.

isCancelled

public final boolean isCancelled()
Returns true if this task was cancelled before it completed normally. If you are calling cancel(boolean) on the task, the value returned by this method should be checked periodically from doInBackground(Object[]) to end the task as soon as possible.

返回:
true if task was cancelled before it completed
另请参见:
cancel(boolean)

cancel

public final boolean cancel(boolean mayInterruptIfRunning)

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 when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

Calling this method will result in onCancelled(Object) being invoked on the UI thread after doInBackground(Object[]) returns. Calling this method guarantees that onPostExecute(Object) is never invoked. After invoking this method, you should check the value returned by isCancelled() periodically from doInBackground(Object[]) to finish the task as early as possible.

参数:
mayInterruptIfRunning - true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete.
返回:
false if the task could not be cancelled, typically because it has already completed normally; true otherwise
另请参见:
isCancelled(), onCancelled(Object)

get

public final Result get()
                 throws java.lang.InterruptedException,
                        java.util.concurrent.ExecutionException
Waits if necessary for the computation to complete, and then retrieves its result.

返回:
The computed result.
抛出:
java.util.concurrent.CancellationException - If the computation was cancelled.
java.util.concurrent.ExecutionException - If the computation threw an exception.
java.lang.InterruptedException - If the current thread was interrupted while waiting.

get

public final Result get(long timeout,
                        java.util.concurrent.TimeUnit unit)
                 throws java.lang.InterruptedException,
                        java.util.concurrent.ExecutionException,
                        java.util.concurrent.TimeoutException
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.

参数:
timeout - Time to wait before cancelling the operation.
unit - The time unit for the timeout.
返回:
The computed result.
抛出:
java.util.concurrent.CancellationException - If the computation was cancelled.
java.util.concurrent.ExecutionException - If the computation threw an exception.
java.lang.InterruptedException - If the current thread was interrupted while waiting.
java.util.concurrent.TimeoutException - If the wait timed out.

execute

public final AsyncTask<Params,Progress,Result> execute(Params... params)
Executes the task with the specified parameters. The task returns itself (this) so that the caller can keep a reference to it.

Note: this function schedules the task on a queue for a single background thread or pool of threads depending on the platform version. When first introduced, AsyncTasks were executed serially on a single background thread. Starting with android.os.Build.VERSION_CODES#DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting android.os.Build.VERSION_CODES#HONEYCOMB, tasks are back to being executed on a single thread to avoid common application errors caused by parallel execution. If you truly want parallel execution, you can use the executeOnExecutor(java.util.concurrent.Executor, Params...) version of this method with THREAD_POOL_EXECUTOR; however, see commentary there for warnings on its use.

This method must be invoked on the UI thread.

参数:
params - The parameters of the task.
返回:
This instance of AsyncTask.
抛出:
java.lang.IllegalStateException - If getStatus() returns either AsyncTask.Status.RUNNING or AsyncTask.Status.FINISHED.
另请参见:
executeOnExecutor(java.util.concurrent.Executor, Object[]), execute(Runnable)

executeOnExecutor

public final AsyncTask<Params,Progress,Result> executeOnExecutor(java.util.concurrent.Executor exec,
                                                                 Params... params)
Executes the task with the specified parameters. The task returns itself (this) so that the caller can keep a reference to it.

This method is typically used with THREAD_POOL_EXECUTOR to allow multiple tasks to run in parallel on a pool of threads managed by AsyncTask, however you can also use your own Executor for custom behavior.

Warning: Allowing multiple tasks to run in parallel from a thread pool is generally not what one wants, because the order of their operation is not defined. For example, if these tasks are used to modify any state in common (such as writing a file due to a button click), there are no guarantees on the order of the modifications. Without careful work it is possible in rare cases for the newer version of the data to be over-written by an older one, leading to obscure data loss and stability issues. Such changes are best executed in serial; to guarantee such work is serialized regardless of platform version you can use this function with SERIAL_EXECUTOR.

This method must be invoked on the UI thread.

参数:
exec - The executor to use. THREAD_POOL_EXECUTOR is available as a convenient process-wide thread pool for tasks that are loosely coupled.
params - The parameters of the task.
返回:
This instance of AsyncTask.
抛出:
java.lang.IllegalStateException - If getStatus() returns either AsyncTask.Status.RUNNING or AsyncTask.Status.FINISHED.
另请参见:
execute(Object[])

execute

public static void execute(java.lang.Runnable runnable)
Convenience version of execute(Object...) for use with a simple Runnable object. See execute(Object[]) for more information on the order of execution.

另请参见:
execute(Object[]), executeOnExecutor(java.util.concurrent.Executor, Object[])