The Android documentation says the following about IntentService:
[IntentService] creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about multi-threading.
But in the example that follows, they use a synchronized block in method onHandleIntent as if it is expected to be executed simultaneously.
protected void onHandleIntent(Intent intent) {
synchronized (this) {
Some operations...
}
}
Why are they using synchronized here? Am I missing something?
In the example I am seeing, they are using wait() in the onHandleIntent() to sleep for 5 seconds. When you call wait(), you have to hold the lock on the object– that’s why they use the synchronize().
So the synchronize() isn’t really significant, it’s just a detail of the sample work they’ve chosed for the example.