【问题标题】:Will two intent services generate two different worker threads or not?两个意图服务是否会生成两个不同的工作线程?
【发布时间】:2017-10-13 13:19:05
【问题描述】:

我想使用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);

【问题讨论】:

    标签: java android multithreading android-intentservice


    【解决方案1】:

    只是我想知道这两个意图服务将生成两个单独的 工作线程或第二个将等待第一个完成。

    两者都将彼此独立运行。第二个不会等待第一个完成。 尽管每个 IntentService 将共享相同的工作实例。因此,假设如果您多次调用startService(intentOne);,您对该特定服务的请求将获得queued。来自here

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

    【讨论】:

      【解决方案2】:

      多个IntentService可以同时运行。他们将在单独的线程上并行执行任务。你可以看看——Can multiple Android IntentServices run at the same time?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多