Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are separate intent services queued on the same thread?

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

like image 930
Chirag Avatar asked May 25 '16 06:05

Chirag


People also ask

Does intent Service run on main thread?

intent service always runs on the worker thread triggered from the main thread. There is a chance of blocking the main thread. tasks will be performed on a queue basis i.e, first come first serve basis. No need to stop the service, it will stop automatically.

Does IntentService run on background thread?

The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness.

What is difference between intent and intent Service?

IntentService Service is a base class for IntentService Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

Can an intent Service execute multiple tasks sequentially?

The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.


1 Answers

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

like image 194
agomes Avatar answered Sep 28 '22 01:09

agomes