Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AsyncTask with ThreadPoolExecutor

I am a little confused on ThreadPoolExecutor. Here is what I am trying to accomplish:

I have a list-view which is populated with an image icon to the left and a short description to its right. I have all of the text already defined statically, however i want to get the icons from the the web. I already know how to get the image from a url, however I dont want to spawn 'n' amount of threads per icon to grab the icon image. So i read up on threadpoolexecutor for asynctask and I am not sure how to go about it. can you guys give me a head start? do I have to create a threadpoolexecutor and use asynctasks within it?

My list is like this.. without the icons.

___________________________
[icon][a short description]
____________________________
[icon][a short description]
_____________________________
[icon][a short description]
_____________________________
[icon][a short description]
____________________________
like image 865
HAxxor Avatar asked Jun 09 '11 23:06

HAxxor


2 Answers

I would recommend just using AsyncTask and not worry about the ThreadPoolExecutor, which requires HoneyComb anyway. AsyncTask has a pool of threads that will be reused for all of your list items. It won't create a bunch of threads per icon.

Just keep in mind that your tasks will be running in parallel on Android 2.x and to keep track of any synchronization issues your code might have.

like image 54
Steve Prentice Avatar answered Oct 13 '22 08:10

Steve Prentice


You will need to use method executeOnExecutor() to start it with your own executor.

There seems to be number of gotchas, so read carefully the entire page for AsyncTask.

So yes, you can create your own ThreadPoolExecutor and use it with AsyncTask.

like image 21
Alex Gitelman Avatar answered Oct 13 '22 08:10

Alex Gitelman