About Android 3.0 after asynctask default single threading analysis

Source: Internet
Author: User

In the case of Android requires a lot of background operations, often used to asynctask this class, such as loading network pictures, access to the interface of the server, the general use of the situation is to instantiate a Asynctask object Mtask, The abstract method of the replication Asynctask Doinbackgroud and so on, finally executes the task.execute (params), and then can be conveniently on the UI thread to obtain the results of the background thread execution;

The final trigger in asynctask execution is to send the task to the line pool thread_pool_executor to execute , the submitted task runs in parallel with the thread pool, However, these rules changed after 3.0, and after 3.0 the submitted task is serially run, performing one task before executing the next!

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;
</pre><p></p><pre>
public static final Executor thread_pool_executor = new Threadpoolexecutor (core_pool_size, Maximum_pool_size, Keep_ Alive,timeunit.seconds, Spoolworkqueue, sthreadfactory);

3.0 before the thread constructor core thread has 5, at the same time the maximum number of threads can not exceed 128, line constructor threads are running in parallel;


However, after 3.0, the direct call to execute (params) triggers the sdefaultexecutor Execute (runnable) method 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;PRI vate static final int keep_alive = 1;

public static void Execute (Runnable Runnable) {        sdefaultexecutor.execute (Runnable);    }

Look at the difference between this sdefaultexecutor and the original thread_pool_executor thread pool,sdefaultexecutor is actually pointing An example of Serialexecutor, from the name is a sequential execution of the executor;

public Static final Executor serial_executor = new Serialexecutor ();p rivate static volatile Executor Sdefaultexecutor = serial_ex Ecutor; private static class Serialexecutor implements Executor {final arraydeque<runnable> mtasks = new arraydeque& Lt        Runnable> ();        Runnable mactive; Public synchronized void Execute (final Runnable R) {Mtasks.offer (new Runnable () {public void R                    Un () {try {r.run ();                    } finally {Schedulenext ();            }                }            });            if (mactive = = null) {Schedulenext ();                }} protected synchronized void Schedulenext () {if ((mactive = Mtasks.poll ()) = null) {            Thread_pool_executor.execute (mactive); }        }    }

parsing Serialexecutor, when a task is submitted, executes execute once (), adds a runnable to Mtasks, at which time mactive is null, then Schedulenext () is executed, Point Mactive to the runbale that you just added, commit to Thread_pool_executor, and execute the following code in the thread pool;

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

when Asynctask commits a large number of tasks, it repeats the previous process, the task is added to the Mtasks, and after the first task is submitted, mactive is no longer null, and the subsequent task must wait for the previous task to run until the Run method is finished, which is try{ } In the statement block, the last task will not call finally after it finishes executing
The following Schedulenext () takes the next task out of the mtasks and executes;


After analyzing the above code, now for 3.0 asynctask by default, there is only one thread order execution principle is clear;


What if the task you want to commit is executed in parallel? This is still more useful in the network picture display;

Asynctask also provides us with another way to start

Public final Asynctask<params, Progress, result> executeonexecutor (Executor exec,params ... Params)

Here you can specify a custom EXECUTOR, instead of using serialexecutor, and if you like, you can use the original thread_pool_executor, so you can ensure that multiple tasks are executed in parallel. ;




About Android 3.0 after asynctask default single threading analysis

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.