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
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.
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.
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.
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.
The IntentService does the following:
- Creates a default worker thread that executes all intents delivered to onStartCommand() separate from your application's main thread.
- Creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about
multi-threading- 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With