I have an IntentService class that can be started from various places in a complex application - Activities, background Threads, other Services. I'd like to keep a counter of how many times the service was invoked. I use a private int variable within my IntentService class to keep track of that, I increment it from my onStartCommand(...) method.
It just occured to me, as the onStartCommand(...) method can be called from various asynchronous threads, that this might not be a thread-safe solution. So the question is, do I need to wrap the access to this counter variable in a synchronized block, or does the IntentService implementation of onStartCommand(...) takes care of this for me?
As a note, I know I could safely increment the variable from onHandleIntent(...), but I'd need a count on the actual requests and not on the executed intents.
It just occured to me, as the onStartCommand(...) method can be called from various asynchronous threads, that this might not be a thread-safe solution.
onStartCommand()
is always called on the main application thread in any service. You cannot be called with onStartCommand()
in two threads simultaneously.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With