/** * Get the current limiter in a thread safe way. If the limiter does not exist it will be * created in a thread safe way. In addition the limiters handling task count will be * incremented in expectation for execution. If not accessing for execution * {@link #currentLimiters} should just be accessed directly. * * @param taskKey Key used to identify execution limiter * @return Container with limiter and associated state data */ protected LimiterContainer getLimiterContainer(Object taskKey) { return currentLimiters.compute(taskKey, (k, v) -> { if (v == null) { v = new LimiterContainer(taskKey, makeLimiter(subPoolName + (addKeyToThreadName ? taskKey.toString() : ""))); } // must increment while in compute to prevent early removal v.handlingTasks.incrementAndGet(); return v; }); }
/** * Get the current limiter in a thread safe way. If the limiter does not exist it will be * created in a thread safe way. In addition the limiters handling task count will be * incremented in expectation for execution. If not accessing for execution * {@link #currentLimiters} should just be accessed directly. * * @param taskKey Key used to identify execution limiter * @return Container with limiter and associated state data */ protected LimiterContainer getLimiterContainer(Object taskKey) { return currentLimiters.compute(taskKey, (k, v) -> { if (v == null) { v = new LimiterContainer(taskKey, makeLimiter(subPoolName + (addKeyToThreadName ? taskKey.toString() : ""))); } // must increment while in compute to prevent early removal v.handlingTasks.incrementAndGet(); return v; }); }
/** * Submit a callable to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#submit(Callable)} * * @param <TT> type of result returned from the future * @param taskKey object key where {@code equals()} will be used to determine execution thread * @param task Callable to be executed * @return Future to represent when the execution has occurred and provide the result from the callable */ public <TT> ListenableFuture<TT> submit(Object taskKey, Callable<TT> task) { ArgumentVerifier.assertNotNull(taskKey, "taskKey"); ArgumentVerifier.assertNotNull(task, "task"); LimiterContainer lc = getLimiterContainer(taskKey); ListenableRunnableFuture<TT> rf = new ListenableFutureTask<>(false, task, lc.limiter); lc.submit(rf); return rf; }
/** * Submit a callable to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#submit(Callable)} * * @param <TT> type of result returned from the future * @param taskKey object key where {@code equals()} will be used to determine execution thread * @param task Callable to be executed * @return Future to represent when the execution has occurred and provide the result from the callable */ public <TT> ListenableFuture<TT> submit(Object taskKey, Callable<TT> task) { ArgumentVerifier.assertNotNull(taskKey, "taskKey"); ArgumentVerifier.assertNotNull(task, "task"); LimiterContainer lc = getLimiterContainer(taskKey); ListenableRunnableFuture<TT> rf = new ListenableFutureTask<>(false, task, lc.limiter); lc.submit(rf); return rf; }
protected void doSchedule(Object taskKey, Runnable task, ListenableFuture<?> lf, long delayInMs) { LimiterContainer lc = getLimiterContainer(taskKey); lc.limiter.doSchedule(lc.wrap(task), lf, delayInMs); }
/** * Provide a task to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#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) { ArgumentVerifier.assertNotNull(taskKey, "taskKey"); ArgumentVerifier.assertNotNull(task, "task"); getLimiterContainer(taskKey).execute(task); }
protected void doSchedule(Object taskKey, Runnable task, ListenableFuture<?> lf, long delayInMs) { LimiterContainer lc = getLimiterContainer(taskKey); lc.limiter.doSchedule(lc.wrap(task), lf, delayInMs); }
/** * Provide a task to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#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) { ArgumentVerifier.assertNotNull(taskKey, "taskKey"); ArgumentVerifier.assertNotNull(task, "task"); getLimiterContainer(taskKey).execute(task); }