@Override protected SubmitterSchedulerLimiter getLimiter(int parallelCount, boolean limitFutureListenersExecution) { return new SubmitterSchedulerLimiter(scheduler, parallelCount, limitFutureListenersExecution); }
@Override public void run() { if (canRunTask()) { // we can run in the thread we already have lrw.run(); } else { addToQueue(lrw); } }
@Override protected void initialRecurringSchedule(RecurringWrapper rw, long initialDelay) { // first cleanup if needed Iterator<WeakReference<RecurringWrapper>> it = recurringTasks.iterator(); while (it.hasNext()) { if (it.next().get() == null) { it.remove(); } } recurringTasks.add(new WeakReference<>(rw)); super.initialRecurringSchedule(rw, initialDelay); }
@Override public <T> ListenableFuture<T> submitScheduled(Callable<T> task, long delayInMs) { ArgumentVerifier.assertNotNull(task, "task"); ListenableFutureTask<T> ft = new ListenableFutureTask<>(false, task, this); doSchedule(ft, ft, delayInMs); return ft; }
protected void initialRecurringSchedule(RecurringWrapper rw, long initialDelay) { ArgumentVerifier.assertNotNegative(initialDelay, "initialDelay"); if (initialDelay == 0) { executeOrQueueWrapper(rw); } else { scheduler.schedule(rw.delayRunnable, initialDelay); } }
@Override public ListenableFuture<?> submitScheduled(Runnable task, long delayInMs) { return submitScheduled(task, null, delayInMs); }
/** * Adds a task to either execute (delay zero), or schedule with the provided delay. No safety * checks are done at this point, so only provide non-null inputs. * * @param task Task for execution * @param delayInMs delay in milliseconds, greater than or equal to zero */ protected void doSchedule(Runnable task, ListenableFuture<?> future, long delayInMs) { if (delayInMs == 0) { executeOrQueue(task, future); } else { scheduler.schedule(new DelayedExecutionRunnable(task), delayInMs); } }
/** * Schedule a fixed delay recurring task to run. The recurring delay time will be from the * point where execution has finished. So the execution frequency is the * {@code recurringDelay + runtime} for the provided task. * <p> * Unlike {@link java.util.concurrent.ScheduledExecutorService} if the task throws an exception, * subsequent executions are NOT suppressed or prevented. So if the task throws an exception on * every run, the task will continue to be executed at the provided recurring delay (possibly * throwing an exception on each execution). * <p> * The key is used to identify this threads execution limit. Tasks with matching keys will be * limited concurrent execution to the level returned by {@link #getMaxConcurrencyPerKey()}. * <p> * See also: {@link SubmitterScheduler#scheduleWithFixedDelay(Runnable, long, long)} * * @param taskKey Key to use for identifying execution limit * @param task runnable to be executed * @param initialDelay delay in milliseconds until first run * @param recurringDelay delay in milliseconds for running task after last finish */ public void scheduleWithFixedDelay(Object taskKey, Runnable task, long initialDelay, long recurringDelay) { ArgumentVerifier.assertNotNull(taskKey, "taskKey"); LimiterContainer lc = getLimiterContainer(taskKey); // we don't wrap the task here because it is recurring, this limiter can never be removed lc.limiter.scheduleWithFixedDelay(task, initialDelay, recurringDelay); }
/** * Schedule a fixed rate recurring task to run. The recurring delay will be the same, * regardless of how long task execution takes. A given runnable will not run concurrently * (unless it is submitted to the scheduler multiple times). Instead of execution takes longer * than the period, the next run will occur immediately (given thread availability in the pool). * <p> * Unlike {@link java.util.concurrent.ScheduledExecutorService} if the task throws an exception, * subsequent executions are NOT suppressed or prevented. So if the task throws an exception on * every run, the task will continue to be executed at the provided recurring delay (possibly * throwing an exception on each execution). * <p> * The key is used to identify this threads execution limit. Tasks with matching keys will be * limited concurrent execution to the level returned by {@link #getMaxConcurrencyPerKey()}. * <p> * See also: {@link SubmitterScheduler#scheduleAtFixedRate(Runnable, long, long)} * * @param taskKey Key to use for identifying execution limit * @param task runnable to be executed * @param initialDelay delay in milliseconds until first run * @param period amount of time in milliseconds between the start of recurring executions */ public void scheduleAtFixedRate(Object taskKey, Runnable task, long initialDelay, long period) { ArgumentVerifier.assertNotNull(taskKey, "taskKey"); LimiterContainer lc = getLimiterContainer(taskKey); // we don't wrap the task here because it is recurring, this limiter can never be removed lc.limiter.scheduleAtFixedRate(task, initialDelay, period); }
@Override public <T> ListenableFuture<T> submitScheduled(Callable<T> task, long delayInMs) { ArgumentVerifier.assertNotNull(task, "task"); ListenableFutureTask<T> ft = new ListenableFutureTask<>(false, task, this); doSchedule(ft, ft, delayInMs); return ft; }
@Override protected void doAfterRunTasks() { nextRunTime += period; long nextDelay = nextRunTime - Clock.accurateForwardProgressingMillis(); if (nextDelay < 1) { executeOrQueueWrapper(this); } else { scheduler.schedule(delayRunnable, nextDelay); } } }
@Override public ListenableFuture<?> submitScheduled(Runnable task, long delayInMs) { return submitScheduled(task, null, delayInMs); }
/** * Adds a task to either execute (delay zero), or schedule with the provided delay. No safety * checks are done at this point, so only provide non-null inputs. * * @param task Task for execution * @param delayInMs delay in milliseconds, greater than or equal to zero */ protected void doSchedule(Runnable task, ListenableFuture<?> future, long delayInMs) { if (delayInMs == 0) { executeOrQueue(task, future); } else { scheduler.schedule(new DelayedExecutionRunnable(task), delayInMs); } }
/** * Schedule a fixed delay recurring task to run. The recurring delay time will be from the * point where execution has finished. So the execution frequency is the * {@code recurringDelay + runtime} for the provided task. * <p> * Unlike {@link java.util.concurrent.ScheduledExecutorService} if the task throws an exception, * subsequent executions are NOT suppressed or prevented. So if the task throws an exception on * every run, the task will continue to be executed at the provided recurring delay (possibly * throwing an exception on each execution). * <p> * The key is used to identify this threads execution limit. Tasks with matching keys will be * limited concurrent execution to the level returned by {@link #getMaxConcurrencyPerKey()}. * <p> * See also: {@link SubmitterScheduler#scheduleWithFixedDelay(Runnable, long, long)} * * @param taskKey Key to use for identifying execution limit * @param task runnable to be executed * @param initialDelay delay in milliseconds until first run * @param recurringDelay delay in milliseconds for running task after last finish */ public void scheduleWithFixedDelay(Object taskKey, Runnable task, long initialDelay, long recurringDelay) { ArgumentVerifier.assertNotNull(taskKey, "taskKey"); LimiterContainer lc = getLimiterContainer(taskKey); // we don't wrap the task here because it is recurring, this limiter can never be removed lc.limiter.scheduleWithFixedDelay(task, initialDelay, recurringDelay); }
/** * Schedule a fixed rate recurring task to run. The recurring delay will be the same, * regardless of how long task execution takes. A given runnable will not run concurrently * (unless it is submitted to the scheduler multiple times). Instead of execution takes longer * than the period, the next run will occur immediately (given thread availability in the pool). * <p> * Unlike {@link java.util.concurrent.ScheduledExecutorService} if the task throws an exception, * subsequent executions are NOT suppressed or prevented. So if the task throws an exception on * every run, the task will continue to be executed at the provided recurring delay (possibly * throwing an exception on each execution). * <p> * The key is used to identify this threads execution limit. Tasks with matching keys will be * limited concurrent execution to the level returned by {@link #getMaxConcurrencyPerKey()}. * <p> * See also: {@link SubmitterScheduler#scheduleAtFixedRate(Runnable, long, long)} * * @param taskKey Key to use for identifying execution limit * @param task runnable to be executed * @param initialDelay delay in milliseconds until first run * @param period amount of time in milliseconds between the start of recurring executions */ public void scheduleAtFixedRate(Object taskKey, Runnable task, long initialDelay, long period) { ArgumentVerifier.assertNotNull(taskKey, "taskKey"); LimiterContainer lc = getLimiterContainer(taskKey); // we don't wrap the task here because it is recurring, this limiter can never be removed lc.limiter.scheduleAtFixedRate(task, initialDelay, period); }
@Override public SubmitterScheduler makeSubmitterScheduler(int poolSize, boolean prestartIfAvailable) { SubmitterScheduler scheduler = schedulerFactory.makeSubmitterScheduler(poolSize, prestartIfAvailable); return new SubmitterSchedulerLimiter(scheduler, poolSize); } }
@Override public void run() { if (canRunTask()) { // we can run in the thread we already have lrw.run(); } else { addToQueue(lrw); } }
protected void doSchedule(Object taskKey, Runnable task, ListenableFuture<?> lf, long delayInMs) { LimiterContainer lc = getLimiterContainer(taskKey); lc.limiter.doSchedule(lc.wrap(task), lf, delayInMs); }
@Override protected void initialRecurringSchedule(RecurringWrapper rw, long initialDelay) { // first cleanup if needed Iterator<WeakReference<RecurringWrapper>> it = recurringTasks.iterator(); while (it.hasNext()) { if (it.next().get() == null) { it.remove(); } } recurringTasks.add(new WeakReference<>(rw)); super.initialRecurringSchedule(rw, initialDelay); }
protected void initialRecurringSchedule(RecurringWrapper rw, long initialDelay) { ArgumentVerifier.assertNotNegative(initialDelay, "initialDelay"); if (initialDelay == 0) { executeOrQueueWrapper(rw); } else { scheduler.schedule(rw.delayRunnable, initialDelay); } }