2

I've got a listview with the list of some user's facebook friends, with each row showing the name and the picture of the friend. To get the picture from Facebook I launch a thread using ASyncTask, following Gilles Debunne's post "Multithreading For Performance".

When I scroll down the list until the end, the number of threads launched can be huge, which makes everything very slow until all threads finish their task.

How can I limit the number of threads?

Thanks

Julien

jul
  • 36,404
  • 64
  • 191
  • 318

2 Answers2

3

Right now, unless you are willing to limit yourself to API Level 11 (Android 3.0) and higher, you cannot limit the number of threads used by AsyncTask. IIRC, it will use up to 20 threads maximum.

If you want fewer than that, you will need to create your own thread pool mechanism and use that instead of AsyncTask. Or, clone the code from AsyncTask into your own project and modify the characteristics of its own thread pool.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Ok thanks. I'll use LazyList, described here: http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview. – jul Mar 19 '11 at 19:58
0

You could use a ThreadPoolExecutor to limit the number of threads in the pool used for the tasks.

typo.pl
  • 8,812
  • 2
  • 28
  • 29