Android Asynctask Analytics Internal implementation

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/jjwwmlp456/article/details/38623563

sdk3.0, using the internal thread pool, multithreading concurrently executes. Thread pool size equals 5, up to 128

After sdk3.0, a thread is executed using the default serial thread pool, and the next thread is executed sequentially. Sdk3.0<=current version <= sdk4.3 when the thread pool size equals 5, up to 128

sdk4.4 thread pool size equals CPU Count + 1, max CPU count * 2 + 1

There are two implementations of the thread pool after sdk3.0, and the default is the Serial thread pool

1  Public Static FinalExecutor Serial_executor =NewSerialexecutor (); 2  Public Static FinalExecutor Thread_pool_executor3=NewThreadpoolexecutor (core_pool_size, Maximum_pool_size, Keep_alive,4 timeunit.seconds, Spoolworkqueue, sthreadfactory); 5 Private Static volatileExecutor Sdefaultexecutor =Serial_executor; 6  Public Static voidSetdefaultexecutor (Executor exec) {//set the default thread pool7Sdefaultexecutor =exec; 8}

Serialexecutor, using a synchronous lock, execute one thread at a time

1 Private Static classSerialexecutorImplementsExecutor {2         FinalArraydeque<runnable> Mtasks =NewArraydeque<runnable>(); 3 Runnable mactive; 4   5          Public synchronized voidExecuteFinalRunnable R) {  6Mtasks.offer (NewRunnable () {7                  Public voidrun () {8                     Try {  9 R.run (); Ten}finally {   One Schedulenext ();  A                     }   -                 }   -             });  the             if(Mactive = =NULL) {   - Schedulenext ();  -             }   - }   +    -         protected synchronized voidSchedulenext () { +             if((mactive = Mtasks.poll ())! =NULL) {   A Thread_pool_executor.execute (mactive);  at             }   -         }   -}

Thread_pool_executor Concurrent Thread pool

Asynctask.setdefaultexecutor (Asynctask.thread_pool_executor); Set up to use the concurrency thread pool

The params is used in the doinbackground (params ... params);

Asynctask.executeonexecutor (executor, params); You can customize the thread pool using both of these methods

Android Asynctask Analytics Internal implementation

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.