About Android 3.0 after asynctask default single thread analysis

Source: Internet
Author: User

Under Android A lot of background operations are needed in case. Typically used to asynctask this class. For example, Network load image. Access the server interface. The general situation is to use one example of a Asynctask object mtask, copy Asynctask abstract method Doinbackgroud, and so on, and finally run Task.execute (params), Then it is possible to get the running result of the background thread conveniently on the UI thread;

The asynctask operation is finally triggered by handing the task to the line pool thread_pool_executor to run , and the submitted task runs in parallel to the thread pool. However, these rules changed after 3.0, and the tasks submitted after 3.0 were serially run. Run one task before running the next!

First look at the Code 3.0 once;

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 Once thread constructor core thread has 5, the maximum number of threads that exist at the same time cannot exceed 128. Thread constructor threads are executed in parallel.


However, after 3.0, the direct call to execute (params) triggers the sdefaultexecutor Execute (runnable) method. Rather than 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 Serialexecutor An example, from the name is a sequential operation 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); }        }    }

Analyze Serialexecutor when submitting a task. Run execute once () and add a runnable to Mtasks. Mactive is null at this time. It then runs Schedulenext (), points the mactive to the runbale you just joined, and commits to the Thread_pool_executor, then runs the following code in the thread pool.

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

When Asynctask commits a large number of tasks. Will repeat the previous process. Tasks are added to Mtasks, after the first task is submitted, mactive is no longer null, then the task is assumed to be run until the previous task run method runs, that is, the try{} statement block in the run (), the previous task is finished running, Before the finally is called
The following Schedulenext () takes the next task out of the mtasks to run.


After analyzing the above code, now for the 3.0 after the Asynctask by default the same time there is only one thread sequence running principle to understand clearly;


Suppose the task you want to commit is running in parallel? This is still more practical in the network picture display;

Asynctask also provides us with a second way to start

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

Here you can specify your own definition of the EXECUTOR, instead of using serialexecutor, assuming that the happy words can also be used directly with the original Thread_pool_executor, so that multiple tasks can be run in parallel .




Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

About Android 3.0 after asynctask default single thread analysis

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.