Android AsyncTask internal implementation of analysis, androidasynctask

Source: Internet
Author: User

Android AsyncTask internal implementation of analysis, androidasynctask

Before sdk3.0, the internal thread pool was used for multi-thread concurrent execution. The thread pool size is equal to 5 and the maximum size is 128.

After sdk3.0, use the default serial thread pool to execute a thread and then execute the next thread in sequence. When sdk3.0 <= current version <= sdk4.3, the thread pool size is equal to 5 and the maximum size is 128.

After sdk4.4, the thread pool size equals to cpu count + 1, and the maximum value is cpu count * 2 + 1

Sdk3.0 has two thread pools. The default is the Serial thread pool.

Public static final Executor SERIAL_EXECUTOR = new SerialExecutor (); public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor (CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit. SECONDS, sPoolWorkQueue, sThreadFactory); private static volatile Executor = SERIAL_EXECUTOR; public static void setDefaultExecutor (Executor exec) {// sets the default thread pool sDefaultExecutor = exec ;}
SerialExecutor, which uses a synchronization lock to execute one thread at a time

private static class SerialExecutor implements Executor {        final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();        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);            }        }    }

THREAD_POOL_EXECUTOR concurrent Thread Pool

Asynctask. setDefaultExecutor (AsyncTask. THREAD_POOL_EXECUTOR); // sets the concurrent thread pool.

Asynctask.exe cuteOnExecutor (executor); // you can customize the thread pool to use these two methods.


Which of the following threads calls the android AsyncTask method?

This is simple and generally involves three methods,
1. onPreExecute (),
Called before high-Load Code Execution, usually used to display a progress bar and execute it in the main thread

2. doInBackGround ():
OnPreExecute () is called after execution. This method usually puts high load code, such as remote requests and massive data loading. You do not need to create a new thread to wrap this method AsyncTask (or subclass) this method is automatically called in the new thread.

3. onPostExecute (Result ),
Call after doInBackground is complete. Generally, it sets the result and cancels the progress bar displayed in the first method.

OnProgressUpdate () is generally used to update the progress bar displayed in the first method. What download 50% 51%...

In short, subclass AsyncTask. You don't have to worry about the thread issue. In the main thread, you can directly subclass the new AsyncTask and call execute. You must call execute in the main thread. Also, these are the life cycle methods of AsyncTask. Do not call them by yourself.

Android AsyncTask Blocking

Before android 1.6, asycntask was implemented by a single thread, 1.6 to 3.0 were implemented by the thread pool, and 3.0 was changed to a single thread considering possible concurrency issues, however, you can use executeOnExecutor to switch to the thread pool mode.

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.