Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

multithreading - Android - Async Task behavior in 2.3.3 and 4.0 OS

I am testing an application where we have a list view with list of images retrieved over network. When i run the application on android device 2.3.3 (WIFI speed 512 KBPS) and check the DDMS (Thread Viewer), the number of threads keeps increasing till 50 from 25. But when i test the same application on device 4.0 (WIFI speed 5 MBPS), number of threads did not increase.

Can anyone help me understand why this is happening ? Is it due to android OS difference or any other reason ?

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Are you useing AsyncTask. After Android 3.0, the default behavior of AsyncTask is execute in a single thread using SERIAL_EXECUTOR.

If you want AsyncTask run concurrently on any system version, you may use this code.

AsyncTask task = new YourTask();
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
    task.execute(params);
} else {
    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
}

Pre OS 1.6 - Multiple Async Tasks gets executed in sequence. OS 1.6 till OS 2.3 - Async Tasks run in parallel. From 3.0 - Again, Async Tasks gets executed in sequence.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...