为什么在此IntentService示例中使用了同步块?

toc*_*777 4 android

Android文档说明了以下关于IntentService的内容:

[IntentService]创建一个工作队列,一次将一个intent传递给onHandleIntent()实现,因此您不必担心多线程.

但是在下面的示例中,它们在方法onHandleIntent中使用了一个synchronized块,就好像它可以同时执行一样.

protected void onHandleIntent(Intent intent) {
    synchronized (this) {
        Some operations...
    }
}
Run Code Online (Sandbox Code Playgroud)

他们为什么在这里使用同步?我错过了什么吗?

ant*_*oft 6

在我看到的例子中,他们在onHandleIntent()中使用wait()来休眠5秒.当你调用wait()时,你必须保持对象的锁定 - 这就是他们使用synchronize()的原因.

因此,synchronize()并不重要,它只是他们为示例选择的示例工作的详细信息.