java.lang.Object
net.i2p.util.SimpleTimer2.TimedEvent
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
AccessFilter.Purger, AccessFilter.Syncer, ACKTimer, BackgroundChecker.Checker, BandwidthGraph, BandwidthHistory, Blocklist.VersionNotifier, ByteCache.GlobalCleanup, ClientConnectionRunner.Rerequest, ClientManager.ClientTimestamper, CoalesceStatsEvent, CommSystemFacadeImpl.NetMonitor, CommSystemFacadeImpl.Timestamper, Connection.AckDupEvent, Connection.ActivityTimer, Connection.ConEvent, Connection.DisconnectEvent, Connection.FloorCheckEvent, Connection.PacedPacketEvent, Connection.ResendPacketEvent, Connection.RetransmitEvent, Connection.TLProbeEvent, ConnectionAcceptor.Cleaner, ConnectionHandler.SynReaper, ConnectionManager.BanExpiry, ConnectionManager.PingFailed, ConnThrottler.Cleaner, ConnThrottler.Cleaner, ConsoleUpdateManager.StatusCleaner, ConsoleUpdateManager.TaskCleaner, CountryLookup.Lookup, CountryLookup.QueueAll, DeadlockDetector, DecayingBloomFilter.DecayEvent, DecayingBloomFilter.DecayHourlyEvent, DelayedDeleter.Checker, DHTNodes.Cleaner, DHTTracker.Cleaner, FloodfillNetworkDatabaseFacade.BatchedSearchTimeoutProcessor, FloodfillNetworkDatabaseFacade.DelayedPublish, FloodThrottler.Cleaner, FragmentHandler.RemoveFailed, I2PSessionImpl.LookupExpiration, I2PSessionImpl.VerifyUsage, I2PSessionImpl2.RemoveExpired, I2PSnarkUtil.Unbanlist, I2PTunnelHTTPServer.BodyWatchdog, I2PTunnelRunner.InitialResponseWatchdog, IdleChecker, IdleCloser.Checker, IdleTunnelMonitor, IntroductionManager.DelayIntro, IntroductionManager.DelaySend, IPThrottler.Cleaner, JobQueueScaler.AutoReleaseEvent, KRPC.Cleaner, KRPC.Explorer, KRPC.ReplyWaiter, LoadClientAppsJob.DelayedRunClient, LookupBanHammer.Cleaner, LookupThrottler.Cleaner, MarkLiveliness, MessageOutputStream.Flusher, NegativeLookupCache.Cleaner, NewsTimerTask, NTCPConnection.DelayedCloser, NTCPConnection.NTCP2FailState, OutboundCache.OCMOSJCacheCleaner, OutboundMessageRegistry.CleanupTask, PacketQueue.RemoveExpired, ParticipatingThrottler.Cleaner, ParticipatingThrottler.Disconnector, PeerCoordinator.CheckEvent, PeerCoordinator.RerequestEvent, PeerManager.MissingRouterInfoDrain, PeerManager.Reorg, PeerState2.ACKTimer, PeerStateDestroyed.ACKTimer, PeerStateDestroyed.KillTimer, PeerTestEvent, PeerTestManager.CharlieTimer, PeerTestManager.ContinueTimer, PeerTestManager.DelaySend, PeerTestManager.DelayTest, PeerTestManager.RemoveTest, PersistentDataStore.Disconnector, RatchetSKM.CleanupEvent, RefreshRoutersJob.Disconnector, Republish, RequestThrottler.Cleaner, RequestThrottler.Disconnector, ReseedChecker.StatusCleaner, RouterThrottleImpl.ResetStatus, SecurityManager.Sweeper, SessionIdleTimer, SimpleByteCache.GlobalCleanup, SnarkManager.DestCycle, SnarkManager.DestCycleRestart, SnarkManager.Disconnector, SnarkManager.Register, SocketTimeout, Subscriber.Expire, TCBShare.CleanEvent, Tcpbw100.PeriodicTimer, TempDirScanner, TrackerClient.Runner, TransientSessionKeyManager.CleanupEvent, TransportImpl.CleanupUnreachable, TransportManager.OutboundMaintainerEvent, TransportManager.UpdatePorts, TransportManager.UPnPRefresher, Tuner, TunnelController.PKFChecker, TunnelDispatcher.PeriodicCleanup, TunnelGateway.DelayedFlush, TunnelGatewayPumper.Requeue, TunnelPool.DeferredRefreshEvent, TunnelPool.LeaseSetRepublishEvent, TunnelPoolManager.ConditionalOutboundStartup, TunnelPoolManager.DelayedPoolCleanup, UDPTrackerClient.ReplyWaiter, UDPTransport.ExpirePeerEvent, UDPTransport.PingIntroducers, UDPTransport.RebuildEvent, UDPTransport.RemoveDropList, UpdateRunner.Timeout, UpdateRunner.Watcher, UPnPManager.DelayedCallback, UPnPManager.Rescanner
Enclosing class:
SimpleTimer2

public abstract static class SimpleTimer2.TimedEventextends Object implements Runnable
Base class for timer events. Extend this and use schedule()/cancel() directly instead of going through SimpleTimer2.

Synchronization is on this to avoid queue duplicates. schedule() is idempotent if already scheduled. reschedule() and forceReschedule(long) replace the existing timer.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
    Whether this was cancelled during RUNNING state.
    private ScheduledFuture<?>
    The pending schedule for this event, null if not scheduled.
    private int
    Random jitter added to the delay, in ms.
    private Log
    Logger for problems running the event.
    private long
    Absolute time this event should run next time.
    private SimpleTimer2
    The timer this event is scheduled on.
    private boolean
    Whether this was scheduled during RUNNING state.
    State of the current event.
    protected static final int
    Default fuzz threshold in milliseconds.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Create a new timed event without scheduling.
    Create a new timed event.
    TimedEvent(SimpleTimer2 pool, long timeoutMs)
    Create a new timed event and automatically schedules it.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Cancel the timed event.
    private boolean
    Validate state before running.
    private void
    Runs the event's doPass() and reschedules it.
    void
    forceReschedule(long timeoutMs)
    Always use the new time - ignores fuzz.
    private void
    Shared init for all constructors.
    private void
    logTimingAndStats(long before)
    Log execution duration and periodic stats.
    void
    reschedule(long timeoutMs)
    Use the earliest of the new time and the old time May be called from within timeReached(), but schedule() is better there.
    void
    reschedule(long timeoutMs, boolean useEarliestTime)
    May be called from within timeReached(), but schedule() is better there.
    void
    run()
    void
    schedule(long timeoutMs)
    Schedule this event.
    void
    setFuzz(int fuzz)
    Reschedule threshold in ms.
    void
    Timer pool for this event.
    abstract void
    Called when this event's scheduled time arrives.
    Timer summary for log messages.
    private void
    Update state after timeReached() completes.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • _log

      private Log _log
      Logger for problems running the event.
    • _pool

      private SimpleTimer2 _pool
      The timer this event is scheduled on.
    • _fuzz

      private int _fuzz
      Random jitter added to the delay, in ms.
    • DEFAULT_FUZZ

      protected static final int DEFAULT_FUZZ
      Default fuzz threshold in milliseconds.
      See Also:
    • _future

      private ScheduledFuture<?> _future
      The pending schedule for this event, null if not scheduled.
    • _state

      protected SimpleTimer2.TimedEventState _state
      State of the current event. All access should be under lock.
    • _nextRun

      private long _nextRun
      Absolute time this event should run next time. LOCKING: this.
    • _rescheduleAfterRun

      private boolean _rescheduleAfterRun
      Whether this was scheduled during RUNNING state. LOCKING: this.
    • _cancelAfterRun

      private boolean _cancelAfterRun
      Whether this was cancelled during RUNNING state. LOCKING: this.
  • Constructor Details

    • TimedEvent

      protected TimedEvent()
      Create a new timed event without scheduling. Pool is set later via setPool(SimpleTimer2) or addEvent().
    • TimedEvent

      public TimedEvent(SimpleTimer2 pool)
      Create a new timed event. Must call schedule() later.
      Parameters:
      pool - the timer pool
    • TimedEvent

      public TimedEvent(SimpleTimer2 pool, long timeoutMs)
      Create a new timed event and automatically schedules it.
      Parameters:
      pool - the timer pool
      timeoutMs - timeout in milliseconds
  • Method Details

    • init

      private void init()
      Shared init for all constructors.
    • setFuzz

      public void setFuzz(int fuzz)
      Reschedule threshold in ms. Rescheduling is skipped if the existing and new timeouts differ by less than this value. Default 100ms.
      Parameters:
      fuzz - the fuzz threshold in ms
    • setPool

      public void setPool(SimpleTimer2 pool)
      Timer pool for this event. Must be called before schedule() for events created via the no-arg constructor. Called automatically by SimpleTimer2.addEvent(SimpleTimer2.TimedEvent, long)/addPeriodicEvent(). Not thread-safe to call concurrently with schedule() or cancel().
      Parameters:
      pool - the timer pool
    • schedule

      public void schedule(long timeoutMs)
      Schedule this event. Does nothing if already scheduled. For self-rescheduling periodic events, call from timeReached().
      Parameters:
      timeoutMs - delay in ms
    • reschedule

      public void reschedule(long timeoutMs)
      Use the earliest of the new time and the old time May be called from within timeReached(), but schedule() is better there.
      Parameters:
      timeoutMs - timeout in milliseconds
    • reschedule

      public void reschedule(long timeoutMs, boolean useEarliestTime)
      May be called from within timeReached(), but schedule() is better there.
      Parameters:
      timeoutMs - timeout in milliseconds
      useEarliestTime - if true and already scheduled, use the earlier timeout; if false and already scheduled, use the later
    • forceReschedule

      public void forceReschedule(long timeoutMs)
      Always use the new time - ignores fuzz.
      Parameters:
      timeoutMs - timeout in milliseconds
    • cancel

      public boolean cancel()
      Cancel the timed event. If the event is currently running, cancellation is deferred until timeReached() completes (sets _cancelAfterRun flag).
      Returns:
      true if the event will not execute, false if already idle/cancelled
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • execute

      private void execute()
      Runs the event's doPass() and reschedules it.
    • checkAndPrepareRun

      private boolean checkAndPrepareRun(long now)
      Validate state before running. Returns true if execution should proceed, false if the event was cancelled or needs to be rescheduled (already handled). Must be called inside synchronized(this).
      Parameters:
      now - the current time in ms since the epoch
      Returns:
      true if execution should proceed, false if cancelled or rescheduled
    • updateStateAfterRun

      private void updateStateAfterRun()
      Update state after timeReached() completes. Must be called inside synchronized(this).
    • logTimingAndStats

      private void logTimingAndStats(long before)
      Log execution duration and periodic stats.
      Parameters:
      before - the time the event started running, in ms since the epoch
    • toString

      public String toString()
      Timer summary for log messages.
      Overrides:
      toString in class Object
      Returns:
      the simple class name for log messages
    • timeReached

      public abstract void timeReached()
      Called when this event's scheduled time arrives. Must NOT block. For periodic events, call schedule(period) at the end to self-reschedule.