Analysis on the default single thread of AsyncTask after Android 3.0

Source: Internet
Author: User

Analysis on the default single thread of AsyncTask after Android 3.0

Cute (params), and then the execution results of background threads can be conveniently obtained on the UI thread;

In AsyncTask execution, the final trigger is to hand over the task to THREAD_POOL_EXECUTOR of the thread pool for execution. The submitted tasks run in parallel in the thread pool, but these rules changed after 3.0, tasks submitted after 3.0 are run in serial mode. Only after one task is executed can the next task be executed!

First look at the code before 3.0;

 

private static final int CORE_POOL_SIZE = 5;private static final int MAXIMUM_POOL_SIZE = 128;private static final int KEEP_ALIVE = 10;
 

 

 
public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);

3.0 before that, there were 5 core threads in the thread pool, and the number of concurrent Threads could not exceed 128. the threads in the thread pool were all running in parallel;

 

However, after 3.0, execute (params) is directly called to trigger the execute (runnable) method of sDefaultExecutor, instead of the original THREAD_POOL_EXECUTOR.

 

private static final int CORE_POOL_SIZE = CPU_COUNT + 1;private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;private static final int KEEP_ALIVE = 1;

public static void execute(Runnable runnable) {        sDefaultExecutor.execute(runnable);    }

 

Let's take a look at the difference between this sDefaultExecutor and the original THREAD_POOL_EXECUTOR thread pool. sDefaultExecutor actually points to an instance of SerialExecutor and its name is a sequential executor;

 

public static final Executor SERIAL_EXECUTOR = new SerialExecutor();private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR; private static class SerialExecutor implements Executor {        final ArrayDeque
 
   mTasks = new ArrayDeque
  
   ();        Runnable mActive;        public synchronized void execute(final Runnable r) {            mTasks.offer(new Runnable() {                public void run() {                    try {                        r.run();                    } finally {                        scheduleNext();                    }                }            });            if (mActive == null) {                scheduleNext();            }        }        protected synchronized void scheduleNext() {            if ((mActive = mTasks.poll()) != null) {                THREAD_POOL_EXECUTOR.execute(mActive);            }        }    }
  
 

Analyze SerialExecutor. When you submit a task and execute () once, add a runnable to mTasks. In this case, mActive is null, scheduleNext () is executed, and mActive is directed to the newly added runbale, and submit it to THREAD_POOL_EXECUTOR for execution. Then, the following code will be executed in the thread pool;

try {                        r.run();                    } finally {                        scheduleNext();                    }

When asyncTask submits a large number of tasks, it repeats the previous process and Adds all tasks to mTasks. After the first task is submitted, mActive is no longer Null, if a subsequent task is to be executed, it must wait until the run () method of the previous task is completed, that is, run () in the try {} statement block. After the previous task is executed, finally is called.
The following scheduleNext () extracts the next task from mTasks for execution;

 

After analyzing the above code, we can now understand the principle that there is only one thread for sequential execution in AsyncTask after 3.0 by default;

 

What if the task to be submitted can be executed in parallel? This is useful in network image display;

AsyncTask also provides us with another startup method.
 

public final AsyncTask
 
   executeOnExecutor(Executor exec,Params... params)
 

The custom executor can be specified here, instead of SerialExecutor. If you are happy, you can also directly use the original THREAD_POOL_EXECUTOR, so that multiple tasks can be executed in parallel;

 

 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.