BroadcastReceiver and ReentrantLock. Are there any problems?
-
06-06-2021 - |
Pergunta
I'm developing a clickable widget. I want to use a static java.util.concurrent.locks ReentrantLock so the widget logic is only called once at a time.
But my fear is, that it may be possible in a very rare condition that the lock is not released, because it is killed beforehand because of the 10 seconds lifecycle window.
Are there objections using a ReentrantLock at all? What is the best way to release the lock?
Or maybe is there a a Android option to run it only single threaded?
At the moment I'm thinking about releasing the lock at the end of the onReceive in a finally block or in the finalize method (ouch).
Solução
I want to use a static java.util.concurrent.locks ReentrantLock so the widget logic is only called once at a time.
onUpdate()
of an AppWidgetProvider
is called on the main application thread. By definition, it can only be called "once at a time". You do not need a lock.