android intentService 并发

android intentService concurrency

当我有一个在 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 
        }

Now lets say i call this service multiple times, do the requests get queued ?

是的,假设 "call this service",您的意思是使用 Intent 调用 startService() 来解析此服务。引用 the documentation:

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.