Reputation: 1140
How many Java threads can I run concurrently in an Android Application ? I think this would be an architecture dependent thing so is there a way to determine the same ?
Upvotes: 1
Views: 291
Reputation: 1006744
How many Java threads can I run concurrently in an Android Application ?
That depends on your definition of "run" and "concurrently".
You can start as many threads as you want, subject primarily to memory limitations.
How many threads are executing simultaneously depends on the number of active cores on the device.
I think this would be an architecture dependent thing
Beyond architecture, it also depends on what is all going on, as Android devices power down cores to save battery power, whenever possible. Plus, depending upon what the threads are doing (e.g., blocking on I/O), having more threads than cores is reasonable.
A typical multi-core thread pool sizing algorithm is to use 2n+1 threads, where n is the number of cores. AsyncTask
uses this approach, for example:
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
Here, the thread pool will grow to MAXIMUM_POOL_SIZE
, which is twice the number of cores (availableProcessors()
) plus one.
Upvotes: 2