3

I have multiple subclasses of IntentService. Can anyone please tell me how many such intent services can run in parallel. Thanks

Shaan
  • 31
  • 4

2 Answers2

2

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

DeeV
  • 35,865
  • 9
  • 108
  • 95
1

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.

44kksharma
  • 2,740
  • 27
  • 32