5

I have two intent services - IntentServiceA and IntentServiceB

They have the following class definitions:

public class FirstService extends IntentService {
  public FirstService() {
      super("first_service");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
      Log.d("chi6rag", "first starts");
      for (long i = 0l; i < 10000000l; i++) {
          if (i == 0l) Log.d("chi6rag", "first started");
      }
      Log.d("chi6rag", "first ends");
  }
}

and

public class SecondService extends IntentService {
  public SecondService() {
      super("second_service");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
      Log.d("chi6rag", "second starts");
      for (long i = 0l; i < 10000000l; i++) {
          if (i == 0l) Log.d("chi6rag", "second started");
      }
      Log.d("chi6rag", "second ends");
  }
}

If I execute the following code in my Activity:

startService(new Intent(this, IntentServiceA.class)); startService(new Intent(this, IntentServiceB.class));

I see the following printed in my logs:

D/chi6rag (11734): first starts

D/chi6rag (11734): first started

D/chi6rag (11734): second starts

D/chi6rag (11734): second started

D/chi6rag (11734): first ends

D/chi6rag (11734): second ends

While if one sees the Intent Service Docs, it clearly mentions that it passes one intent at a time to onHandleIntent method of the IntentService

Question: Is there a separate worker thread for each and every intent service? because the expected logs are:

D/chi6rag (11734): first starts

D/chi6rag (11734): first started

D/chi6rag (11734): first ends

D/chi6rag (11734): second starts

D/chi6rag (11734): second started

D/chi6rag (11734): second ends

Chirag
  • 967
  • 8
  • 15
  • 1
    every `IntentService` uses an own `HandlerThread` to serve requests, so if you have two `IntentService`s there are two separate `HandlerThread`s – pskink May 25 '16 at 06:17
  • 2
    "one intent at a time" -- provided those intents are both directed to the same `IntentService` – Karakuri May 25 '16 at 06:19

2 Answers2

5

The IntentService does the following:

  1. Creates a default worker thread that executes all intents delivered to onStartCommand() separate from your application's main thread.
  2. Creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about
    multi-threading
  3. Provides a default implementation of onStartCommand() that sends the intent to the work queue and then to your onHandleIntent() implementation.

Since you have multiple classes extended form IntentService which each has their own specific onHandleIntent. These are handled on separate work queues.

See Extending the Intentservice

agomes
  • 331
  • 2
  • 8
1

Look at the docs: IntentService

All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.

AFAIK, all the request will be handled on a single thread per IntentService. That means if you create many IntentServices, there is no guarantee that which IntentService's thread is executed first.

If you want to run many threads or do work sequentially in a single thread, try to play ExecutorService with Executors.newSingleThreadExecutor() and runnable as in the ExecutorService Examples

Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86