可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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
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.
回答2:
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.
回答3:
You can use the AsyncTaskCompat.executeInParallel for API
An exemple of use :
AsyncTaskCompat.executeParallel(new AsyncTask() { @Override protected Bitmap doInBackground(Void... params) { return MediaStore.Images.Thumbnails.getThumbnail( imageView.getContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MINI_KIND, null); } @Override protected void onPostExecute(Bitmap bitmap) { imageView.setImageBitmap(bitmap); if (bitmap != null) { // Add the image to the memory cache first CACHE.put(id, bitmap); if (listener != null) { listener.onImageLoaded(bitmap); } } } });
enjoy