In my little app, I receive a series of queued onEventXXX(), from the system (not controlled by me).
The timing of those onEventXXX() is not guaranteed. The only thing guaranteed is that they are received in the order in which they were put into the queue.
Inside onEventXXX() I have to fire an operation (O) that cannot start while another process (P) is going on.
But I cannot just discard this "firing an operation (O)". I must wait until that process (P) is complete, then fire it.
IOW, I have to queue or at least postpone that "firing an operation" until that process (P) is complete.
My immediate idea for implementing this was that , instead of firing the operation (O), I would:
But knowing the richness of Android, I am suspecting there is a better way of doing this, already built into the system/API.
If there is a better way to implement the above, what would it be?
Opt to postpone the event if there's an issue related to the date, venue, or your vendors. But you'll likely have to cancel the event if there's a safety or budget concern that you can't resolve within a couple weeks of your original date.
If you're ready to terminate your service agreement, you should be sure to do so in writing. You can either send an email to your service provider or compose a termination letter on business stationery. You should sign this notification using both your official title and the name of your company.
CountDownLatch would be the best solution.
I don't have detail example, but I think you can use it with ease.
Process is like below
If you code like that, you can handle many situations easily.
I use CountDownLatch in asyc unit test. You can see my code here : http://kingori.egloos.com/4554640
Although it's not like your case, but you can grab hint from my code. Also, javadoc of CountDownLatch provides good example.
I would do it this way:
in onEventX, check if process exited, if yes, do your action immediately (you can also omit this step and just do 2nd step)
otherwise, create AsyncTask where you call Process.waitfor() in doInBackground, when it completes, do your action in onPostExecute
With this approach, you call action on UI thread, and as soon as possible.
Do not use timers for this task. Timer is a "polling" method. If your timer would be ticking too fast, you would just waste CPU time. If it's ticking too slow, your action would be called with possible delay.
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