Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 338
Prem Avatar asked Oct 26 '12 03:10

Prem


People also ask

What is the use of asynchronous task in Android?

In this case, the android's asynchronous task becomes handy especially for updating the portion of the UI using background threads. An asynchronous task is one of the several ways to offload the work of main thread to some background thread. While AsyncTask is not the only option, it is simple and quite a common option.

What are the methods of asynctask in Android?

Method of AsyncTask In Android: 1 onPreExecute () –. It invoked on the main UI thread before the task is executed. ... 2 doInBackground (Params) –. This method is invoked on the background thread immediately after onPreExecute () finishes its execution. 3 onProgressUpdate (Progress…) –. ... 4 onPostExecute (Result) –. ...

What are the three types used by asynchronous task in Java?

The three types used by Asynchronous task are the following: 1. TypeOfVarArgParams: Params is the type of the parameters sent to the task upon execution. 2. ProgressValue: Progress is the type of the progress units published during the background computation. 3.

What is asynctask class in Java?

AsyncTask class is used to do background operations that will update the UI (user interface). Mainly we used it for short operations that will not effect on our main thread. AsyncTask class is firstly executed using execute () method.


2 Answers

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.

like image 185
Ponyets Avatar answered Oct 29 '22 18:10

Ponyets


Are you using an AsyncTask to execute the background operation? I think there is a difference between the implementation of the AsyncTask between GB and ICS.

Try to add some debug logging when the thread finishes its work and see if there is a difference between the two versions.

like image 42
dnkoutso Avatar answered Oct 29 '22 19:10

dnkoutso