views:

74

answers:

2

Hi.

I have an android app, where in a list view for each element in list, I load an image from web in a separate thread. So if there are 8 items displayed in list view, activity will try to fire 8 different threads, one for each list item to load an image. As you scroll down the list, the number of threads may increase if the previous threads haven't finished executing.

I am curious to know how many simultaneous threads can a single android app execute in parallel? Is there a limit? I wouldn't expect these threads to cause a ANR over slow internet connection as they are independent? But it seems that ANR does happen and may be it's because app/device run low on resources, so spawning a new activity in UI takes more than 5 seconds which results in an ANR?

Any clues to how I can make responsiveness better on a slow internet connection will be appreciated.

+1  A: 

One way to handle this is to use a thread pool. I don't know if there is a finite limit to the number of simultaneous threads, but creating and destroying them is expensive. With a thread pool, you have a limited number of threads that can perform tasks.

A thread can be reused once it has finished a task, which leads to performance improvements.

If you have more work that needs to be done simultaneously than you have threads to perform it, you need to queue up the work until a thread is free.

Mayra
Note that `AsyncTask` has a thread pool built in.
CommonsWare
So instead of spawning a new thread if I use a async task for loading images in list view, then for each list item, does it uses the same AsyncTask and hence same thread pool or will it use a different Async task and hence different thread pool?
Priyank
@Priyank I believe what CommonsWare means is that AsyncTask makes use of a thread pool as part of its implementation. So when you create an AsyncTask, it may reuse an old thread instead of creating a new one. This helps cut down on object creation, and thus need for gc, etc. Its not so useful if you are really trying to dowload 100 images at the same time, each on a different thread. But, if you download 5, then a few seconds later download another 5 you'll probably see performance improvements.
Mayra
oh okie! That makes sense. Thank you for explaining that.
Priyank
A: 

Later on I found out that my app was hung and slow not because there were too many threads spawned by it. But it was instead because of the fact that I was using Service and not IntentService. And my network IO was happening on main thread in Service. Which means a blocking IO will choke the main thread and phone/app will tend to die and pop up an ANR.

I later on changed my design to run network IO operations in spawned threads in my Services, that put the life back in the app. Everything was smoother as expected.

So whenever your services are resulting in ANR, ensure that if you are not using IntentService(they fire jobs in a separate threads), then you fire blocking operations in new threads.

Hope that helps someone.

Priyank