Search code examples
javaandroidmultithreadingandroid-intentservice

Will two intent services generate two different worker threads or not?


I want to do two separate background tasks with IntentService, and I want to know if both intent services will generate two separate worker threads or second will wait for first to finish.

Ex.

public class IntentServiceOne extends IntentService {
    public IntentServiceOne() {
        super("IntentServiceOne");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
       // code to execute
    }
}

public class IntentServiceSecond extends IntentService {
    public IntentServiceSecond() {
        super("IntentServiceSecond");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
       // code to execute
   }
}

Code from activity:

Intent intentOne=new Intent(this, IntentServiceOne.class);
startService(intentOne);
Intent intentSecond=new Intent(this, IntentServiceSecond.class);
startService(intentSecond);

Solution

  • Just i want to know both intent service will generate two saperate worker thread or second will wait for first to finish.

    Both would run independently from each other. Second will not wait for first to finish. Although Each IntentService would share same worker instance. So suppose if you call startService(intentOne); multiple times your request for this particular service gets queued. From here.

    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.