@Test public void testTasksDoNotRunInParallelOnDelegate() throws Exception { Executor delegate = Executors.newCachedThreadPool(); PoliteExecutor polite = new PoliteExecutor(TAG, delegate, 1); List<Integer> list = new Vector<>(); CountDownLatch latch = new CountDownLatch(TASKS); for (int i = 0; i < TASKS; i++) { int result = i; polite.execute(() -> { try { Thread.sleep(TASKS - result); list.add(result); } catch (InterruptedException e) { fail(); } latch.countDown(); }); } latch.await(); assertEquals(ascendingOrder(), list); }
@Override public void execute(Runnable r) { long submitted = now(); Runnable wrapped = () -> { if (log.isLoggable(FINE)) { long queued = now() - submitted; log.fine("Queue time " + queued + " ms"); } try { r.run(); } finally { scheduleNext(); } }; synchronized (lock) { if (concurrentTasks < maxConcurrentTasks) { concurrentTasks++; delegate.execute(wrapped); } else { queue.add(wrapped); } } }
PoliteExecutor implements Executor { @Override public void execute(Runnable r) { long submitted = now(); Runnable wrapped = () -> { if (log.isLoggable(FINE)) { long queued = now() - submitted; log.fine("Queue time " + queued + " ms"); } try { r.run(); } finally { scheduleNext(); } }; synchronized (lock) { if (concurrentTasks < maxConcurrentTasks) { concurrentTasks++; delegate.execute(wrapped); } else { queue.add(wrapped); } } } }
PoliteExecutor implements Executor { @Override public void execute(Runnable r) { long submitted = now(); Runnable wrapped = () -> { if (log.isLoggable(FINE)) { long queued = now() - submitted; log.fine("Queue time " + queued + " ms"); } try { r.run(); } finally { scheduleNext(); } }; synchronized (lock) { if (concurrentTasks < maxConcurrentTasks) { concurrentTasks++; delegate.execute(wrapped); } else { queue.add(wrapped); } } } PoliteExecutor(String tag, Executor delegate,
			int maxConcurrentTasks); }
PoliteExecutor implements Executor { @Override public void execute(Runnable r) { long submitted = now(); Runnable wrapped = () -> { if (log.isLoggable(FINE)) { long queued = now() - submitted; log.fine("Queue time " + queued + " ms"); } try { r.run(); } finally { scheduleNext(); } }; synchronized (lock) { if (concurrentTasks < maxConcurrentTasks) { concurrentTasks++; delegate.execute(wrapped); } else { queue.add(wrapped); } } } PoliteExecutor(String tag, Executor delegate,
			int maxConcurrentTasks); @Override void execute(Runnable r); }
PoliteExecutor implements Executor { @Override public void execute(Runnable r) { long submitted = now(); Runnable wrapped = () -> { if (log.isLoggable(FINE)) { long queued = now() - submitted; log.fine("Queue time " + queued + " ms"); } try { r.run(); } finally { scheduleNext(); } }; synchronized (lock) { if (concurrentTasks < maxConcurrentTasks) { concurrentTasks++; delegate.execute(wrapped); } else { queue.add(wrapped); } } } PoliteExecutor(String tag, Executor delegate,
			int maxConcurrentTasks); @Override void execute(Runnable r);  }