protected void doSchedule(Object taskKey, Runnable task, ListenableFuture<?> lf, long delayInMs) { LimiterContainer lc = getLimiterContainer(taskKey); lc.limiter.doSchedule(lc.wrap(task), lf, delayInMs); }
@Override public void run() { if (canRunTask()) { // we can run in the thread we already have lrw.run(); } else { addToQueue(lrw); } }
@Override protected void doExecute(Runnable task) { limiterForKey(taskKey, (l) -> l.execute(permits, task)); } }
@Override protected OneTimeTaskWrapper doSchedule(Runnable task, long delayInMillis, TaskPriority priority) { OneTimeTaskWrapper result = noThreadScheduler.doSchedule(task, delayInMillis, priority); if (delayInMillis > 0) { delegateScheduler.schedule(() -> tickTask.signalToRunImmediately(true), delayInMillis); } else { tickTask.signalToRun(); } return result; }
/** * * Construct a new single threaded sub-pool with default task priority behaviors. * * @param delegateScheduler Scheduler to gain CPU time for task execution * @param defaultPriority Default priority for tasks submitted to this pool * @param maxWaitForLowPriorityInMs time low priority tasks to wait if there are high priority tasks ready to run */ public SingleThreadSchedulerSubPool(SchedulerService delegateScheduler, TaskPriority defaultPriority, long maxWaitForLowPriorityInMs) { super(defaultPriority); this.delegateScheduler = delegateScheduler; this.noThreadScheduler = new NoThreadScheduler(defaultPriority, maxWaitForLowPriorityInMs); this.tickTask = new TickTask(delegateScheduler); }
/** * Submit a task to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#submit(Runnable)} * * @param taskKey object key where {@code equals()} will be used to determine execution thread * @param task Task to be executed * @return Future to represent when the execution has occurred */ public ListenableFuture<?> submit(Object taskKey, Runnable task) { return submit(taskKey, task, null); }
/** * Provide a task to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#execute(Runnable)} and * {@link RateLimiterExecutor#execute(Runnable)}. * * @param taskKey object key where {@code equals()} will be used to determine execution thread * @param task Task to be executed */ public void execute(Object taskKey, Runnable task) { execute(1, taskKey, task); }
/** * Exact same as the submit counter part, except you can specify how many permits this task will * require/use (instead of defaulting to 1). The task will be scheduled out as far as necessary * to ensure it conforms to the set rate. * * @param permits resource permits for this task * @param task Runnable to execute when ready * @return Future that will indicate when the execution of this task has completed */ public ListenableFuture<?> submit(double permits, Runnable task) { return submit(permits, task, null); }
/** * Returns an executor implementation where all tasks submitted on this executor will run on the * provided key. Tasks executed on the returned scheduler will be limited by the key * submitted on this instance equally with ones provided through the returned instance. * * @param taskKey object key where {@code equals()} will be used to determine execution thread * @return Executor which will only execute with reference to the provided key */ public SubmitterExecutor getSubmitterExecutorForKey(Object taskKey) { return getSubmitterExecutorForKey(1, taskKey); }
@Override public Runnable getContainedRunnable() { return lrw.getContainedRunnable(); } }
@Override public <T> ListenableFuture<T> submit(Callable<T> task) { return limiter.submit("foo", task); } };
@Override protected void doExecute(Runnable task) { limiterForKey(taskKey, (l) -> l.execute(permits, task)); } }
protected void doSchedule(Object taskKey, Runnable task, ListenableFuture<?> lf, long delayInMs) { LimiterContainer lc = getLimiterContainer(taskKey); lc.limiter.doSchedule(lc.wrap(task), lf, delayInMs); }
@Override public void run() { if (canRunTask()) { // we can run in the thread we already have lrw.run(); } else { addToQueue(lrw); } }
@Override protected OneTimeTaskWrapper doSchedule(Runnable task, long delayInMillis, TaskPriority priority) { OneTimeTaskWrapper result = noThreadScheduler.doSchedule(task, delayInMillis, priority); if (delayInMillis > 0) { delegateScheduler.schedule(() -> tickTask.signalToRunImmediately(true), delayInMillis); } else { tickTask.signalToRun(); } return result; }
/** * Submit a task to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#submit(Runnable)} * * @param taskKey object key where {@code equals()} will be used to determine execution thread * @param task Task to be executed * @return Future to represent when the execution has occurred */ public ListenableFuture<?> submit(Object taskKey, Runnable task) { return submit(taskKey, task, null); }
/** * Provide a task to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#execute(Runnable)} and * {@link RateLimiterExecutor#execute(Runnable)}. * * @param taskKey object key where {@code equals()} will be used to determine execution thread * @param task Task to be executed */ public void execute(Object taskKey, Runnable task) { execute(1, taskKey, task); }
/** * Returns an executor implementation where all tasks submitted on this executor will run on the * provided key. Tasks executed on the returned scheduler will be limited by the key * submitted on this instance equally with ones provided through the returned instance. * * @param taskKey object key where {@code equals()} will be used to determine execution thread * @return Executor which will only execute with reference to the provided key */ public SubmitterExecutor getSubmitterExecutorForKey(Object taskKey) { return getSubmitterExecutorForKey(1, taskKey); }
/** * Provide a task to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#execute(Runnable)} and * {@link RateLimiterExecutor#execute(double, Runnable)}. * * @param permits resource permits for this task * @param taskKey object key where {@code equals()} will be used to determine execution thread * @param task Task to be executed * @return Time in milliseconds task was delayed to maintain rate, or {@code -1} if rejected but handler did not throw */ public long execute(double permits, Object taskKey, Runnable task) { return limiterForKey(taskKey, (l) -> l.execute(permits, task)); }
/** * Provide a task to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#execute(Runnable)} and * {@link RateLimiterExecutor#execute(double, Runnable)}. * * @param permits resource permits for this task * @param taskKey object key where {@code equals()} will be used to determine execution thread * @param task Task to be executed * @return Time in milliseconds task was delayed to maintain rate, or {@code -1} if rejected but handler did not throw */ public long execute(double permits, Object taskKey, Runnable task) { return limiterForKey(taskKey, (l) -> l.execute(permits, task)); }