Why does the IntentService example use a synchronized block?

The Android documentation says the following about IntentService:

[IntentService] creates a work queue that simultaneously passes one intention to your onHandleIntent () implementation, so you don’t have to worry about multithreading.

But in the following example, they use a synchronized block in the onHandleIntent method, as if it was expected to be executed simultaneously.

protected void onHandleIntent(Intent intent) { synchronized (this) { Some operations... } } 

Why are they using sync here? Did I miss something?

+4
source share
1 answer

In the example I see, they use wait () in onHandleIntent () to sleep for 5 seconds. When you call wait (), you have to hold the lock on the object - which is why they use the synchronize () function.

Thus, synchronize () is not very significant, it is just a detail of the sample they were listening to for example.

+6
source

Source: https://habr.com/ru/post/1385613/


All Articles