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
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
FieldsModifier and TypeFieldDescriptionprivate booleanWhether this was cancelled during RUNNING state.private ScheduledFuture<?> The pending schedule for this event, null if not scheduled.private intRandom jitter added to the delay, in ms.private LogLogger for problems running the event.private longAbsolute time this event should run next time.private SimpleTimer2The timer this event is scheduled on.private booleanWhether this was scheduled during RUNNING state.protected SimpleTimer2.TimedEventStateState of the current event.protected static final intDefault fuzz threshold in milliseconds.Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreate a new timed event without scheduling.TimedEvent(SimpleTimer2 pool) Create a new timed event.TimedEvent(SimpleTimer2 pool, long timeoutMs) Create a new timed event and automatically schedules it.Method Summary
Modifier and TypeMethodDescriptionbooleancancel()Cancel the timed event.private booleancheckAndPrepareRun(long now) Validate state before running.private voidexecute()Runs the event's doPass() and reschedules it.voidforceReschedule(long timeoutMs) Always use the new time - ignores fuzz.private voidinit()Shared init for all constructors.private voidlogTimingAndStats(long before) Log execution duration and periodic stats.voidreschedule(long timeoutMs) Use the earliest of the new time and the old time May be called from within timeReached(), but schedule() is better there.voidreschedule(long timeoutMs, boolean useEarliestTime) May be called from within timeReached(), but schedule() is better there.voidrun()voidschedule(long timeoutMs) Schedule this event.voidsetFuzz(int fuzz) Reschedule threshold in ms.voidsetPool(SimpleTimer2 pool) Timer pool for this event.abstract voidCalled when this event's scheduled time arrives.toString()Timer summary for log messages.private voidUpdate state after timeReached() completes.
Field Details
_log
Logger for problems running the event._pool
The timer this event is scheduled on._fuzz
private int _fuzzRandom jitter added to the delay, in ms.DEFAULT_FUZZ
protected static final int DEFAULT_FUZZDefault fuzz threshold in milliseconds.- See Also:
_future
The pending schedule for this event, null if not scheduled._state
State of the current event. All access should be under lock._nextRun
private long _nextRunAbsolute time this event should run next time. LOCKING: this._rescheduleAfterRun
private boolean _rescheduleAfterRunWhether this was scheduled during RUNNING state. LOCKING: this._cancelAfterRun
private boolean _cancelAfterRunWhether this was cancelled during RUNNING state. LOCKING: this.
Constructor Details
TimedEvent
protected TimedEvent()Create a new timed event without scheduling. Pool is set later viasetPool(SimpleTimer2)or addEvent().TimedEvent
Create a new timed event. Must call schedule() later.- Parameters:
pool- the timer pool
TimedEvent
Create a new timed event and automatically schedules it.- Parameters:
pool- the timer pooltimeoutMs- 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
Timer pool for this event. Must be called before schedule() for events created via the no-arg constructor. Called automatically bySimpleTimer2.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 millisecondsuseEarliestTime- 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
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
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.