【问题标题】:android intentService concurrencyandroid intentService并发
【发布时间】:2015-08-11 00:19:14
【问题描述】:

当我有一个在 onHandleIntent 完成之前被多次调用的 intentService 时,在 android 中会发生什么。让我给你举个例子:

假设我有一个如下所示的意图服务:

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

    @Override
    protected void onHandleIntent(Intent intent) {
            // magic happens here but lets pretend it takes 3 mins.
        }

}

现在假设我多次调用此服务,请求是否排队?并发是如何处理的,或者我希望通过在 onHandleIntent 中放置一个同步块来处理它,如下所示:

synchronized (AService.class) {
            //do stuff here 
        }

【问题讨论】:

    标签: android android-intentservice


    【解决方案1】:

    现在假设我多次调用此服务,请求是否排队?

    是的,假设通过“调用此服务”,您的意思是调用 startService() 并带有解析为该服务的 Intent。引用the documentation:

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

    【讨论】:

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