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.
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.