I have multiple subclasses of IntentService. Can anyone please tell me how many such intent services can run in parallel. Thanks
-
As many as threads can run in parallel – Nishant Verma Nov 29 '16 at 11:41
-
can you please give the number – Shaan Nov 29 '16 at 11:44
-
from this link http://stackoverflow.com/questions/24505981/how-many-asynctasks-i-can-run-in-an-single-process-application i got the number 128, is this the number you are referring or else can you please provide the number – Shaan Nov 29 '16 at 11:48
-
The number of threads that can run in parallel depends on number of cores your cpu has – Nishant Verma Nov 29 '16 at 12:51
2 Answers
IntentService
does not run in parallel if it's from the same class. They will run sequentially. In other words, if you had an IntentService
named DownloadService
which downloads a file from a given URL, then you run it like so:
List<String> downloadUrls = getDownloadUrls();
Context ctx = getContext();
Intent baseIntent = new Intent(ctx, DownloadService.class);
for(String url : downloadUrls) {
Intent downloadService = new Intent(baseIntent);
downloadService.putString("downloadUrl", url);
ctx.startService(downloadService);
}
What would happen is the DownloadService
would start once. Then, the first URL would be passed to it, and it would start downloading. Then, after that has finished, the DownloadService
would use the second url. Then the third and so on. It all runs sequentially.
Now, you can have a second IntentService
named UploadService
which similarly would upload a file to a given URL. If you start this multiple times with different parameters, it will also run sequentially to every call. However, it will run parallel to DownloadService
because they are two separate Service
.
EDIT: As far as a limiting number. I'm sure there is one, but you'll likely run out of memory before you reach it (or decide that a hundred different services is not the optimal way to do things).

- 35,865
- 9
- 108
- 95
you can start any no of instances from different subclasses of IntentService in parallel but in reality only N services will be running at a moment where N are no of core in the device and if we run more than N service in parallel then their threads will have context switching where all the running services will share the clock time. And if you start multiple instance of same subclasses of IntentService , then they will run serially and in such situation you need to use Service class instead of IntentService to achieve parallel execution.

- 2,740
- 27
- 32