- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class TimerHandle extends Handle
See Timer handle
-
Field Summary
-
Method Summary
Modifier and Type Method Description intagain()Stop the timer, and if it is repeating restart it using the repeat value as the timeout.voidclose()Close the handle and free up any resources that may be held by it.longgetRepeat()Get the timer repeat value.static longnow(LoopHandle loop)Return the current timestamp in milliseconds.voidsetCloseCallback(CloseCallback callback)Attach aCloseCallback.voidsetRepeat(long repeat)Set the repeat interval value in milliseconds.voidsetTimerFiredCallback(TimerCallback callback)Attach aTimerCallback.intstart(long timeout, long repeat)Start the timer.intstop()Stop the timer, the callback will not be called anymore.
-
Method Details
-
setTimerFiredCallback
Attach aTimerCallback.- Parameters:
callback- A Callback, which will be invoked once per loop iteration, right before thePrepareCallbackhandles.
-
setCloseCallback
Attach aCloseCallback.- Parameters:
callback- A Callback, which will be invoked when check handle is closed.- Throws:
java.lang.IllegalStateException- if this method called more than once.
-
now
Return the current timestamp in milliseconds.The timestamp increases monotonically from some arbitrary point in time. Don't make assumptions about the starting point, you will only get disappointed.
- Returns:
- current timestamp in milliseconds.
-
start
public int start(long timeout, long repeat)Start the timer.If timeout is zero, the callback fires on the next event loop iteration. If repeat is non-zero, the callback fires first after timeout milliseconds and then repeatedly after repeat milliseconds.
The timer will be scheduled to run on the given interval, regardless of the callback execution duration, and will follow normal timer semantics in the case of a time-slice overrun.
For example, if a
50msrepeating timer first runs for17ms, it will be scheduled to run again33mslater. If other tasks consume more than the @{code 33ms} following the first timer callback, then the callback will run as soon as possible.Note
If the repeat value is set from a timer callback it does not immediately take effect. If the timer was non-repeating before, it will have been stopped. If it was repeating, then the old repeat value will have been used to schedule the next timeout.- Parameters:
timeout- timer's timeout value in milliseconds.repeat- timer's timer repeat value in milliseconds.
-
again
public int again()Stop the timer, and if it is repeating restart it using the repeat value as the timeout.If the timer has never been started before it returns UV_EINVAL.
-
getRepeat
public long getRepeat()Get the timer repeat value.- Returns:
- timer value in milliseconds.
-
setRepeat
public void setRepeat(long repeat)Set the repeat interval value in milliseconds.The timer will be scheduled to run on the given interval, regardless of the callback execution duration, and will follow normal timer semantics in the case of a time-slice overrun.
For example, if a 50ms repeating timer first runs for 17ms, it will be scheduled to run again 33ms later. If other tasks consume more than the 33ms following the first timer callback, then the callback will run as soon as possible.
Note
If the repeat value is set from a timer callback it does not immediately take effect. If the timer was non-repeating before, it will have been stopped. If it was repeating, then the old repeat value will have been used to schedule the next timeout.- Parameters:
repeat- timer's repeat value in milliseconds.
-
stop
public int stop()Stop the timer, the callback will not be called anymore.- Returns:
0on success, or an errorcode < 0on failure.
-
close
public void close()Close the handle and free up any resources that may be held by it.CloseCallbackcallback will be invoked right before the close.
-