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