Private static Final int CORE_POOL_SIZE Math.max = ( 2 , Math.min (CPU_COUNT - . 1 , . 4 )); // Maximum number of threads times the number of pool 2 of the core cpu. 1 + Private static Final int MAXIMUM_POOL_SIZE CPU_COUNT * = 2 + 1 ; // non-core number of threads survival time was 30s Private static Final int KEEP_ALIVE_SECONDS = 30 ;
1 2 3
Blocking queue length is 128 Private static Final BlockingQueue <the Runnable> = sPoolWorkQueue new new a LinkedBlockingQueue <the Runnable> ( 128 );
Use in important ways:
1 2 3 4 5 6 7 8
// operation before beginning the task need: the main thread protected void () {} // abstract class must implement. Consuming operation: child thread protected abstract the Result the doInBackground ( the Params ... the params ) ; // progress of the background task execution, for displaying a progress bar: the main thread protected void onProgressUpdate ( Progress ... values ) {} // task after the completion of the operation performed, you need to call: main thread protected void onPostExecute ( the Result Result ) {}
execute execution method:
1 2 3 4 5 6 7 8
The first: we need to pass parameters scheduler and public Final the AsyncTask < Params , Progress, the Result > executeOnExecutor (Executor Exec, Params ... params ) The second: the internal call first way, using the default scheduler, parameters passed to public Final the AsyncTask < the Params , Progress, the Result > execute ( the Params ... the params ) third: The method of runnable default scheduler performs public static void execute (the Runnable runnable)
SerialExecutor by ArrayDeque queue management Runnable when we start a lot of tasks, the first run in the first execute () method when invoked offer ArrayDeque's () method to add incoming Runnable object to the tail of the queue and then determine mActive the object is not equal to null, null is certainly the first run, so call scheduleNext () method. The queues value from the head, and assigned to mActive object and then call THREAD_POOL_EXECUTOR.execute (mActive) method, to perform Runnable object retrieved.
execute and scheduleNext are synchronized modified, these two methods have the same object lock, can not run at the same time, in the execute method, r.run () method is time-consuming tasks sub-thread, when the execution is completed, the final will enter into finally call scheduleNext to remove a top task.
Big Box AsyncTask Source understand mTasks.offer to the task each time are placed in the tail ArrayDeque ensure a first-come, first it is executed.
public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec, Params... params) { if (mStatus != Status.PENDING) { switch (mStatus) { case RUNNING: thrownew IllegalStateException("Cannot execute task:" + " the task is already running."); case FINISHED: thrownew IllegalStateException("Cannot execute task:" + " the task has already been executed " + "(a task can be executed only once)"); } }
private static classInternalHandlerextendsHandler{ public InternalHandler(Looper looper) { super(looper); }
@SuppressWarnings({"unchecked", "RawUseOfParameterizedType"}) public void handleMessage(Message msg) { AsyncTaskResult<?> result = (AsyncTaskResult<?>) msg.obj; switch (msg.what) { caseMESSAGE_POST_RESULT: // There is only one result result.mTask.finish(result.mData[0]); break; caseMESSAGE_POST_PROGRESS: result.mTask.onProgressUpdate(result.mData); break; } } }
In the past has been used AsyncTask, but also know how to call, but did not know how it works, after we analyze the source code can be seen, AsyncTask for us to do a good thread management (abuse of threads in the application, it will cause too many threads causing serious problems), it provides pre-execution, execution progress bar update, after performing the callback operations, facilitated our use.