Java timer problem example analysis
Timer problem
Timer is a basic component. Whether it is user space program development or kernel space program development, it often needs the support of timer as a basic component. The implementation of a timer needs to have the following four basic behaviors: add timer, cancel timer, timer check and expiration execution.
Please design a timer and realize the following three basic behaviors. The function prototype has been given. You can use any programming language to design the data structure and implementation, and support a large number of timers as efficiently as possible:
//Add timer: execute the target operation after a specific time interval
/ / enter 1: interval timer time, in MS
/ / enter 2: expiryaction target action
/ / return: timer ID
StartTimer(Interval,ExpiryAction) -> TimerId
/ / cancel timer
/ / enter: timer ID
StopTimer(TimerId)
/ / timer check
/ / the system will call this function every 10ms
PerTickBookkeeping()
Don't say much, just go to the code:
1)Timer.java:
2)DoTimer.java:
I hope this article can help you