/** * 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); }
/** * 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); }
/** * Submit a task to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#submit(Runnable, Object)} * * @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 Runnable to be executed * @param result Result to be returned from future when task completes * @return Future to represent when the execution has occurred and provide the given result */ public <TT> ListenableFuture<TT> submit(Object taskKey, Runnable task, TT result) { return submit(taskKey, RunnableCallableAdapter.adapt(task, result)); }
/** * Submit a task to be run with a given thread key. * <p> * See also: {@link SubmitterExecutor#submit(Runnable, Object)} * * @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 Runnable to be executed * @param result Result to be returned from future when task completes * @return Future to represent when the execution has occurred and provide the given result */ public <TT> ListenableFuture<TT> submit(Object taskKey, Runnable task, TT result) { return submit(taskKey, RunnableCallableAdapter.adapt(task, result)); }
@Test public void getUnsubmittedTaskCountMapTest() { AbstractKeyedLimiter<?> singleConcurrencyLimiter = makeLimiter(1); String key = StringUtils.makeRandomString(5); BlockingTestRunnable btr = new BlockingTestRunnable(); try { assertTrue(singleConcurrencyLimiter.getUnsubmittedTaskCountMap().isEmpty()); singleConcurrencyLimiter.execute(key, btr); btr.blockTillStarted(); // should not be queued any more assertTrue(singleConcurrencyLimiter.getUnsubmittedTaskCountMap().isEmpty()); for (int i = 1; i < TEST_QTY; i++) { singleConcurrencyLimiter.submit(key, DoNothingRunnable.instance()); Map<?, ?> taskCountMap = singleConcurrencyLimiter.getUnsubmittedTaskCountMap(); assertEquals(1, taskCountMap.size()); assertEquals(i, taskCountMap.get(key)); } } finally { btr.unblock(); } }
@Test public void getUnsubmittedTaskCountTest() { AbstractKeyedLimiter<?> singleConcurrencyLimiter = makeLimiter(1); String key = StringUtils.makeRandomString(5); BlockingTestRunnable btr = new BlockingTestRunnable(); try { assertEquals(0, singleConcurrencyLimiter.getUnsubmittedTaskCount(key)); singleConcurrencyLimiter.execute(key, btr); btr.blockTillStarted(); // should not be queued any more assertEquals(0, singleConcurrencyLimiter.getUnsubmittedTaskCount(key)); for (int i = 1; i < TEST_QTY; i++) { singleConcurrencyLimiter.submit(key, DoNothingRunnable.instance()); assertEquals(i, singleConcurrencyLimiter.getUnsubmittedTaskCount(key)); } } finally { btr.unblock(); } }