Class TimerHandle

java.lang.Object
com.oracle.libuv.Handle
com.oracle.libuv.TimerHandle
All Implemented Interfaces:
java.io.Closeable, java.lang.AutoCloseable

public class TimerHandle
extends Handle
Timer handles are used to schedule callbacks to be called in the future.

See Timer handle

  • Field Summary

    Fields inherited from class com.oracle.libuv.Handle

    loop, pointer
  • Method Summary

    Modifier and Type Method Description
    int again()
    Stop the timer, and if it is repeating restart it using the repeat value as the timeout.
    void close()
    Close the handle and free up any resources that may be held by it.
    long getRepeat()
    Get the timer repeat value.
    static long now​(LoopHandle loop)
    Return the current timestamp in milliseconds.
    void setCloseCallback​(CloseCallback callback)
    Attach a CloseCallback.
    void setRepeat​(long repeat)
    Set the repeat interval value in milliseconds.
    void setTimerFiredCallback​(TimerCallback callback)
    Attach a TimerCallback.
    int start​(long timeout, long repeat)
    Start the timer.
    int stop()
    Stop the timer, the callback will not be called anymore.

    Methods inherited from class com.oracle.libuv.Handle

    equals, hashCode, isClosing, ref, toString, unref

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • setTimerFiredCallback

      public void setTimerFiredCallback​(TimerCallback callback)
      Attach a TimerCallback.
      Parameters:
      callback - A Callback, which will be invoked once per loop iteration, right before the PrepareCallback handles.
    • setCloseCallback

      public void setCloseCallback​(CloseCallback callback)
      Attach a CloseCallback.
      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

      public static long now​(LoopHandle loop)
      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 50ms repeating timer first runs for 17ms, it will be scheduled to run again 33ms later. 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:
      0 on success, or an error code < 0 on failure.
    • close

      public void close()
      Close the handle and free up any resources that may be held by it.

      CloseCallback callback will be invoked right before the close.