/** * Schedule a rate-limit task if necessary. Lock must be acquired before calling this method! */ private void schedule() { schedule(_period); }
@Override public void submit(Task t) { boolean runNow = false; synchronized (this) { if (_period == 0 && _pending.isEmpty() && _runningTasks < _maxRunningTasks) { _runningTasks ++; runNow = true; } else { _pending.add(t); schedule(); } } if (runNow) { t.run(_doneCallback); } }
public void setPeriod(long ms) { Long previous = null; ms = Math.min(_maxPeriod, Math.max(_minPeriod, ms)); synchronized (this) { if (ms != _period) { previous = _period; _period = ms; if (!_pending.isEmpty() && (_task == null || _task.cancel(false))) { long adjustedPeriod = _period; if (_task != null) { long elapsedTime = previous - _task.getDelay(TimeUnit.MILLISECONDS); adjustedPeriod = Math.max(_period - elapsedTime, 0); _task = null; } schedule(adjustedPeriod); } } } if (previous != null) { // Use previous to avoid logging in the synchronized block LOG.debug("Minimum period changed from {} to {}", previous, ms); } }