两个意向服务是否会生成两个不同的工作线程



我想用IntentService执行两个单独的后台任务,我想知道两个意图服务是否会生成两个单独的工作线程,或者第二个将等待第一个完成。

前任。

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
   }
}

来自活动的代码:

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

只是我想知道两个意图服务都会产生两个 saperate 工作线程或第二个线程将等待第一个完成。

两者将彼此独立运行。第二个不会等待第一个完成。尽管每个IntentService将共享相同的辅助角色实例。因此,假设如果您多次调用startService(intentOne);,则对此特定服务的请求会queued。从这里。

所有请求都在单个工作线程上处理 - 它们可能被视为 只要有必要(并且不会阻塞应用程序的主循环), 但一次只会处理一个请求。

多个

IntentService可以同时运行。他们将在单独的线程上并行完成任务。你可以看看它 - 多个Android IntentServices可以同时运行吗?

最新更新