#1069: Deprecated SimpleScheduler and moved functionality into SimpleTimer2

This commit is contained in:
dev
2015-04-06 21:05:24 +00:00
parent 4d8e577ffd
commit facbe8f9a0
34 changed files with 149 additions and 51 deletions

View File

@@ -939,6 +939,7 @@ public class I2PAppContext {
/**
* Use instead of SimpleScheduler.getInstance()
* @since 0.9 to replace static instance in the class
* @deprecated in 0.9.19
*/
public SimpleScheduler simpleScheduler() {
if (!_simpleSchedulerInitialized)
@@ -946,6 +947,9 @@ public class I2PAppContext {
return _simpleScheduler;
}
/**
* @deprecated in 0.9.19
*/
private void initializeSimpleScheduler() {
synchronized (_lock18) {
if (_simpleScheduler == null)

View File

@@ -1353,7 +1353,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
boolean close = Boolean.parseBoolean(_options.getProperty("i2cp.closeOnIdle"));
if (reduce || close) {
updateActivity();
_context.simpleScheduler().addEvent(new SessionIdleTimer(_context, this, reduce, close), SessionIdleTimer.MINIMUM_TIME);
_context.simpleTimer2().addEvent(new SessionIdleTimer(_context, this, reduce, close), SessionIdleTimer.MINIMUM_TIME);
}
}

View File

@@ -118,6 +118,6 @@ class SessionIdleTimer implements SimpleTimer.TimedEvent {
} else {
nextDelay = _minimumTime - (now - lastActivity);
}
_context.simpleScheduler().addEvent(this, nextDelay);
_context.simpleTimer2().addEvent(this, nextDelay);
}
}

View File

@@ -123,7 +123,7 @@ public final class ByteCache {
_maxCached = maxCachedEntries;
_entrySize = entrySize;
_lastOverflow = -1;
SimpleScheduler.getInstance().addPeriodicEvent(new Cleanup(), CLEANUP_FREQUENCY + (entrySize % 777)); //stagger
SimpleTimer2.getInstance().addPeriodicEvent(new Cleanup(), CLEANUP_FREQUENCY + (entrySize % 777)); //stagger
I2PAppContext.getGlobalContext().statManager().createRateStat("byteCache.memory." + entrySize, "Memory usage (B)", "Router", new long[] { 10*60*1000 });
}

View File

@@ -30,6 +30,7 @@ public class SimpleScheduler {
/**
* If you have a context, use context.simpleScheduler() instead
* @deprecated in 0.9.19
*/
public static SimpleScheduler getInstance() {
return I2PAppContext.getGlobalContext().simpleScheduler();
@@ -46,6 +47,7 @@ public class SimpleScheduler {
/**
* To be instantiated by the context.
* Others should use context.simpleTimer() instead
* @deprecated in 0.9.19
*/
public SimpleScheduler(I2PAppContext context) {
this(context, "SimpleScheduler");
@@ -54,6 +56,7 @@ public class SimpleScheduler {
/**
* To be instantiated by the context.
* Others should use context.simpleTimer() instead
* @deprecated in 0.9.19
*/
private SimpleScheduler(I2PAppContext context, String name) {
_log = context.logManager().getLog(SimpleScheduler.class);

View File

@@ -124,6 +124,66 @@ public class SimpleTimer2 {
private ScheduledFuture schedule(TimedEvent t, long timeoutMs) {
return _executor.schedule(t, timeoutMs, TimeUnit.MILLISECONDS);
}
/**
* Queue up the given event to be fired no sooner than timeoutMs from now.
*
* @param event
* @param timeoutMs
*/
public void addEvent(final SimpleTimer.TimedEvent event, long timeoutMs) {
if (event == null)
throw new IllegalArgumentException("addEvent null");
new TimedEvent(SimpleTimer2.getInstance(), timeoutMs) {
@Override
public void timeReached() {
event.timeReached();
}
};
}
/**
* 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).
*
* @param delay run the first iteration of this event after delay ms
* @param timeoutMs run subsequent iterations of this event every timeoutMs ms
*/
public void addPeriodicEvent(final SimpleTimer.TimedEvent event, final long timeoutMs) {
new PeriodicTimedEvent(SimpleTimer2.getInstance(), timeoutMs) {
@Override
public void timeReached() {
event.timeReached();
}
};
}
/**
* 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).
*
* @param delay run the first iteration of this event after delay ms
* @param timeoutMs run subsequent iterations of this event every timeoutMs ms
*/
public void addPeriodicEvent(final SimpleTimer.TimedEvent event, final long delay, final long timeoutMs) {
new PeriodicTimedEvent(SimpleTimer2.getInstance(), delay, timeoutMs) {
@Override
public void timeReached() {
event.timeReached();
}
};
}
/**
* state of a given TimedEvent
@@ -141,6 +201,7 @@ public class SimpleTimer2 {
CANCELLED
};
/**
* Similar to SimpleTimer.TimedEvent but users must extend instead of implement,
* and all schedule and cancel methods are through this class rather than SimpleTimer2.
@@ -228,7 +289,6 @@ public class SimpleTimer2 {
break;
case SCHEDULED: // nothing
}
}
/**
@@ -403,5 +463,36 @@ public class SimpleTimer2 {
" Completed: " + _executor.getCompletedTaskCount() +
" Queued: " + _executor.getQueue().size();
}
public static abstract class PeriodicTimedEvent extends TimedEvent {
private long _timeoutMs;
/**
* Schedule periodic event
*
* @param timeoutMs run subsequent iterations of this event every timeoutMs ms
*/
public PeriodicTimedEvent(SimpleTimer2 pool, long timeoutMs) {
super(pool, timeoutMs);
_timeoutMs = timeoutMs;
}
/**
* Schedule periodic event
*
* @param delay run the first iteration of this event after delay ms
* @param timeoutMs run subsequent iterations of this event every timeoutMs ms
*/
public PeriodicTimedEvent(SimpleTimer2 pool, long delay, long timeoutMs) {
super(pool, delay);
_timeoutMs = timeoutMs;
}
@Override
public void run() {
super.run();
schedule(_timeoutMs);
}
}
}