@Test public void testMultiSubmitters() { final AsyncSemaphore<Runnable> asyncSemaphore = new AsyncSemaphore<>(2, executor, executor::submit); final AtomicInteger count = new AtomicInteger(); final AtomicInteger concurrency = new AtomicInteger(); final CountDownLatch startLatch = new CountDownLatch(1); final CountDownLatch completionLatch = new CountDownLatch(100); for (int i = 0; i < 100; i++) { executor.execute(() -> { Uninterruptibles.awaitUninterruptibly(startLatch, 1, TimeUnit.MINUTES); asyncSemaphore.submit(() -> { count.incrementAndGet(); int currentConcurrency = concurrency.incrementAndGet(); assertLessThanOrEqual(currentConcurrency, 2); Uninterruptibles.sleepUninterruptibly(1, TimeUnit.MILLISECONDS); concurrency.decrementAndGet(); completionLatch.countDown(); }); }); } startLatch.countDown(); Uninterruptibles.awaitUninterruptibly(completionLatch, 1, TimeUnit.MINUTES); Assert.assertEquals(count.get(), 100); }
public ListenableFuture<?> submit(T task) { QueuedTask<T> queuedTask = new QueuedTask<>(task); queuedTasks.add(queuedTask); acquirePermit(); return queuedTask.getCompletionFuture(); }
AsyncSemaphore { public ListenableFuture<?> submit(T task) { QueuedTask<T> queuedTask = new QueuedTask<>(task); queuedTasks.add(queuedTask); acquirePermit(); return queuedTask.getCompletionFuture(); } }
AsyncSemaphore { public ListenableFuture<?> submit(T task) { QueuedTask<T> queuedTask = new QueuedTask<>(task); queuedTasks.add(queuedTask); acquirePermit(); return queuedTask.getCompletionFuture(); } AsyncSemaphore(int maxPermits, Executor submitExecutor, Function<T, ListenableFuture<?>> submitter); }
AsyncSemaphore { public ListenableFuture<?> submit(T task) { QueuedTask<T> queuedTask = new QueuedTask<>(task); queuedTasks.add(queuedTask); acquirePermit(); return queuedTask.getCompletionFuture(); } AsyncSemaphore(int maxPermits, Executor submitExecutor, Function<T, ListenableFuture<?>> submitter); ListenableFuture<?> submit(T task); }
AsyncSemaphore { public ListenableFuture<?> submit(T task) { QueuedTask<T> queuedTask = new QueuedTask<>(task); queuedTasks.add(queuedTask); acquirePermit(); return queuedTask.getCompletionFuture(); } AsyncSemaphore(int maxPermits, Executor submitExecutor, Function<T, ListenableFuture<?>> submitter); ListenableFuture<?> submit(T task);  }