-
Enhancement
-
Resolution: Won't Do
-
Major
-
None
-
None
Currently the TimeService registers a task that runs every (say) 500 ms. It gets the current time and updates a field. Threads which get the time from the TimeService get the value of that field, so System.currentTimeMillis() is not called too much.
However, when no thread needs to get the time for a period of time, the TimeService nevertheless calls System.currentTimeMillis() every 500 ms, and we have an additional task in the timer.
SOLUTION
- Remove that task which periodically gets the current time
- Add a timestamp field (System.nanoTime()) which gets set when System.currentTimeMillis() is called
- Add a field current_time which is set by System.currentTimeMillis()
- When a thread wants to get the current time from the TimeService, it compares the current time with the timestamp and, if 500 ms have elapsed, calls System.currentTimeMillis(), sets the timestamp and current_time fields and returns the time
- If the time elapsed since the last update is less than 500 ms, it returns current_time
There has to be synchronization around getting the current time (System.currentTimeMillis()), but this could be done with a CAS and a busy loop.