Menu

Timer Enhancement

We are working through the last bits of the Bashing Game, and this gave rise to another feature: timer cancellation.

Short story: you may be in a situation where you are doing other "stuff" while the timer is active, and now you're "done" but the timer is still running, and you still want the TimerCallback to occur.

In the current scheme, this is problematic; you must hold a reference to TimerService in order to call its cancel method. Since a major design goal is to not have to do that, we took care of it.

The current TimerService.cancel is now called TimerService.unregister which now acts as the dual of TimerService.register, and everywhere that called the first one, now calls the second one, e.g in Pipelines.

There is a new implementation of TimerService.cancel that handles the cancellation portion, and does so in such a way that you get to receive one final TimerCallback with last==true and elapsed is whatever partial time at that point, and should be less-than the duration.

The final piece of this feature is a new interface TimerCancel and a method TimerConfig.obtainToken to get one to hold onto. This makes it pretty simple.

  • during the call to your TimerCallback.setConfig if you want to be able to cancel, call TimerConfig.obtainToken and save the reference for an appropriate moment.
  • when you want to cancel your timer, call TimerCancel.cancel. You should release this reference as well; it won't be valid after the final TimerCallback occurs.
  • in your TimerCallback.execute you can determine if you were cancelled by comparing the elapsed value to the duration.
  • remember TimerService is a separate task (thread), and you are in a race condition with the actual timer expiration! The TimerService may have already sent the "final" TimerCallback to the pipeline, and in this case the cancel will have no effect.
Posted by g-dollar 2014-12-06 Labels: feature

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.