net.i2p.util

## Class SimpleTimer2

- java.lang.Object
- - net.i2p.util.SimpleTimer2

- Direct Known Subclasses:
    RetransmissionTimer

      public class SimpleTimer2
      extends Object

  Simple event scheduler - toss an event on the queue and it gets fired at the appropriate time. The method that is fired however should NOT block (otherwise they b0rk the timer). This rewrites the old SimpleTimer to use the java.util.concurrent.ScheduledThreadPoolExecutor. SimpleTimer has problems with lock contention; this should work a lot better. This supports cancelling and arbitrary rescheduling. If you don't need that, use SimpleScheduler instead. SimpleTimer is deprecated, use this or SimpleScheduler.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `SimpleTimer2.TimedEvent` Similar to SimpleTimer.TimedEvent but users must extend instead of implement, and all schedule and cancel methods are through this class rather than SimpleTimer2.

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `` | `SimpleTimer2(I2PAppContext context)` To be instantiated by the context.
    `protected` | `SimpleTimer2(I2PAppContext context, String name)` To be instantiated by the context.
    `protected` | `SimpleTimer2(I2PAppContext context, String name, boolean prestartAllThreads)` To be instantiated by the context.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addEvent(SimpleTimer.TimedEvent event, long timeoutMs)` Queue up the given event to be fired no sooner than timeoutMs from now.
    `void` | `addPeriodicEvent(SimpleTimer.TimedEvent event, long timeoutMs)` Schedule periodic event The TimedEvent must not do its own rescheduling.
    `void` | `addPeriodicEvent(SimpleTimer.TimedEvent event, long delay, long timeoutMs)` Schedule periodic event The TimedEvent must not do its own rescheduling.
    `static SimpleTimer2` | `getInstance()` If you have a context, use context.simpleTimer2() instead
    `void` | `stop()` Stops the SimpleTimer.
    `String` | `toString()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### SimpleTimer2

          public SimpleTimer2(I2PAppContext context)

      To be instantiated by the context. Others should use context.simpleTimer2() instead
      Parameters:
        `context` - the I2P application context

    - #### SimpleTimer2

          protected SimpleTimer2(I2PAppContext context,
                                 String name)

      To be instantiated by the context. Others should use context.simpleTimer2() instead
      Parameters:
        `context` - the I2P application context
        `name` - the timer name

    - #### SimpleTimer2

          protected SimpleTimer2(I2PAppContext context,
                                 String name,
                                 boolean prestartAllThreads)

      To be instantiated by the context. Others should use context.simpleTimer2() instead
      Parameters:
        `context` - the I2P application context
        `name` - the timer name
        `prestartAllThreads` - whether to prestart all threads

  - ### Method Detail

    - #### addEvent

          public void addEvent(SimpleTimer.TimedEvent event,
                               long timeoutMs)

      Queue up the given event to be fired no sooner than timeoutMs from now. For transition from SimpleScheduler. Uncancellable. New code should use SimpleTimer2.TimedEvent.
      Parameters:
        `event` - to be run once
        `timeoutMs` - run after this delay

    - #### addPeriodicEvent

          public void addPeriodicEvent(SimpleTimer.TimedEvent event,
                                       long timeoutMs)

      Schedule periodic event The TimedEvent must not do its own rescheduling. As all Exceptions are caught in run(), these will not prevent subsequent executions (unlike SimpleTimer, where the TimedEvent does its own rescheduling). For transition from SimpleScheduler. Uncancellable. New code should use SimpleTimer2.TimedEvent.
      Parameters:
        `event` - the event to run periodically
        `timeoutMs` - run subsequent iterations of this event every timeoutMs ms, 5000 minimum
      Throws:
        `IllegalArgumentException` - if timeoutMs less than 5000

    - #### addPeriodicEvent

          public void addPeriodicEvent(SimpleTimer.TimedEvent event,
                                       long delay,
                                       long timeoutMs)

      Schedule periodic event The TimedEvent must not do its own rescheduling. As all Exceptions are caught in run(), these will not prevent subsequent executions (unlike SimpleTimer, where the TimedEvent does its own rescheduling). For transition from SimpleScheduler. Uncancellable. New code should use SimpleTimer2.TimedEvent.
      Parameters:
        `event` - the event to run periodically
        `delay` - run the first iteration of this event after delay ms
        `timeoutMs` - run subsequent iterations of this event every timeoutMs ms, 5000 minimum
      Throws:
        `IllegalArgumentException` - if timeoutMs less than 5000

    - #### getInstance

          public static SimpleTimer2 getInstance()

      If you have a context, use context.simpleTimer2() instead
      Returns:
        the global SimpleTimer2 instance

    - #### stop

          public void stop()

      Stops the SimpleTimer. Subsequent executions should not throw a RejectedExecutionException. Cannot be restarted.

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`
      Returns:
        the timer name