Refine search
public class BoundedExecutor { private final Executor exec; private final Semaphore semaphore; public BoundedExecutor(Executor exec, int bound) { this.exec = exec; this.semaphore = new Semaphore(bound); } public void submitTask(final Runnable command) throws InterruptedException, RejectedExecutionException { semaphore.acquire(); try { exec.execute(new Runnable() { public void run() { try { command.run(); } finally { semaphore.release(); } } }); } catch (RejectedExecutionException e) { semaphore.release(); throw e; } } }
@Test public void testRunningJobIsInterruptedAfterShutdownNow() throws InterruptedException { BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(); ExecutorService service = Executors.newSingleThreadExecutor(); try { PartitionedOrderedExecutor executor = new PartitionedOrderedExecutor(queue, service); final Semaphore jobSemaphore = new Semaphore(0); final Semaphore testSemaphore = new Semaphore(0); final AtomicBoolean interrupted = new AtomicBoolean(); executor.submit(() -> { testSemaphore.release(); try { jobSemaphore.acquire(); } catch (InterruptedException e) { interrupted.set(true); } }); testSemaphore.acquireUninterruptibly(); assertThat(executor.shutdownNow(), empty()); assertThat(executor.awaitTermination(2, MINUTES), is(true)); assertThat(executor.isShutdown(), is(true)); assertThat(executor.isTerminated(), is(true)); assertThat(jobSemaphore.availablePermits(), is(0)); assertThat(interrupted.get(), is(true)); } finally { service.shutdown(); } }
@Test public void testFaultsDoNotGetToEvictionAdvisor() throws StoreAccessException { final Semaphore semaphore = new Semaphore(0); final OnHeapStoreForTests<String, String> store = newStore(SystemTimeSource.INSTANCE, noAdvice()); ExecutorService executor = Executors.newCachedThreadPool(); try { executor.submit(() -> store.getOrComputeIfAbsent("prime", key -> { semaphore.acquireUninterruptibly(); return new OnHeapValueHolder<String>(0, 0, false) { @Override public String get() { return key; } }; })); while (!semaphore.hasQueuedThreads()); store.put("boom", "boom"); } finally { semaphore.release(1); executor.shutdown(); } }
public static void main(String[] args) { ExecutorService executor = Executors.newCachedThreadPool(); Semaphore semaphore = new Semaphore(3); Runnable r = () -> { try { System.out.println("Trying to acquire - " + Thread.currentThread().getName()); if (semaphore.tryAcquire(2, TimeUnit.SECONDS)) { // use-get resource // simulate work in progress System.out.println("Acquired - " + Thread.currentThread().getName()); Thread.sleep(2000); System.out.println("Done - " + Thread.currentThread().getName()); } } catch (InterruptedException e) { e.printStackTrace(); } finally { semaphore.release(); } }; for (int i = 0; i < 4; i++) { executor.execute(r); } executor.shutdown(); } }
@Test public void testRunningJobIsInterruptedAfterShutdownNow() throws InterruptedException { BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(); ExecutorService service = Executors.newSingleThreadExecutor(); try { PartitionedUnorderedExecutor executor = new PartitionedUnorderedExecutor(queue, service, 1); final Semaphore jobSemaphore = new Semaphore(0); final Semaphore testSemaphore = new Semaphore(0); final AtomicBoolean interrupted = new AtomicBoolean(); executor.submit(() -> { testSemaphore.release(); try { jobSemaphore.acquire(); } catch (InterruptedException e) { interrupted.set(true); } }); testSemaphore.acquireUninterruptibly(); assertThat(executor.shutdownNow(), empty()); assertThat(executor.awaitTermination(2, MINUTES), is(true)); assertThat(executor.isShutdown(), is(true)); assertThat(executor.isTerminated(), is(true)); assertThat(jobSemaphore.availablePermits(), is(0)); assertThat(interrupted.get(), is(true)); } finally { service.shutdown(); } }
@Test public void testQueuedJobIsStoppedAfterShutdownNow() throws InterruptedException { BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(); ExecutorService service = Executors.newSingleThreadExecutor(); try { PartitionedUnorderedExecutor executor = new PartitionedUnorderedExecutor(queue, service, 1); final Semaphore jobSemaphore = new Semaphore(0); final Semaphore testSemaphore = new Semaphore(0); executor.submit(() -> { testSemaphore.release(); jobSemaphore.acquireUninterruptibly(); }); final AtomicBoolean called = new AtomicBoolean(); executor.submit(() -> called.set(true)); testSemaphore.acquireUninterruptibly(); assertThat(executor.shutdownNow(), hasSize(1)); assertThat(executor.awaitTermination(100, MILLISECONDS), is(false)); assertThat(executor.isShutdown(), is(true)); assertThat(executor.isTerminated(), is(false)); jobSemaphore.release(); assertThat(executor.awaitTermination(2, MINUTES), is(true)); assertThat(executor.isShutdown(), is(true)); assertThat(executor.isTerminated(), is(true)); assertThat(jobSemaphore.availablePermits(), is(0)); assertThat(called.get(), is(false)); } finally { service.shutdown(); } }
@Test public void testRunningJobIsInterruptedAfterShutdownNow() throws InterruptedException { ExecutorService worker = Executors.newSingleThreadExecutor(); try { PartitionedScheduledExecutor executor = new PartitionedScheduledExecutor(scheduler, worker); final Semaphore jobSemaphore = new Semaphore(0); final Semaphore testSemaphore = new Semaphore(0); final AtomicBoolean interrupted = new AtomicBoolean(); executor.submit(() -> { testSemaphore.release(); try { jobSemaphore.acquire(); } catch (InterruptedException e) { interrupted.set(true); } }); testSemaphore.acquireUninterruptibly(); assertThat(executor.shutdownNow(), empty()); assertThat(executor.awaitTermination(2, MINUTES), is(true)); assertThat(executor.isShutdown(), is(true)); assertThat(executor.isTerminated(), is(true)); assertThat(jobSemaphore.availablePermits(), is(0)); assertThat(interrupted.get(), is(true)); } finally { worker.shutdown(); } }
@Test public void testQueuedJobIsStoppedAfterShutdownNow() throws InterruptedException { ExecutorService worker = Executors.newSingleThreadExecutor(); try { PartitionedScheduledExecutor executor = new PartitionedScheduledExecutor(scheduler, worker); final Semaphore jobSemaphore = new Semaphore(0); final Semaphore testSemaphore = new Semaphore(0); executor.submit(() -> { testSemaphore.release(); jobSemaphore.acquireUninterruptibly(); }); final AtomicBoolean called = new AtomicBoolean(); executor.submit(() -> called.set(true)); testSemaphore.acquireUninterruptibly(); assertThat(executor.shutdownNow(), hasSize(1)); assertThat(executor.awaitTermination(100, MILLISECONDS), is(false)); assertThat(executor.isShutdown(), is(true)); assertThat(executor.isTerminated(), is(false)); jobSemaphore.release(); assertThat(executor.awaitTermination(2, MINUTES), is(true)); assertThat(executor.isShutdown(), is(true)); assertThat(executor.isTerminated(), is(true)); assertThat(jobSemaphore.availablePermits(), is(0)); assertThat(called.get(), is(false)); } finally { worker.shutdown(); } }
public void run(int iterations, int limit, Executor executor, Supplier<Long> latency) { AtomicInteger requests = new AtomicInteger(); AtomicInteger busy = new AtomicInteger(); AtomicInteger counter = new AtomicInteger(); Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> { System.out.println("" + counter.incrementAndGet() + " total=" + requests.getAndSet(0) + " busy=" + busy.get()); }, 1, 1, TimeUnit.SECONDS); Semaphore sem = new Semaphore(limit, true); for (int i = 0; i < iterations; i++) { requests.incrementAndGet(); busy.incrementAndGet(); executor.execute(() -> { try { sem.acquire(); TimeUnit.MILLISECONDS.sleep(latency.get()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { sem.release(); busy.decrementAndGet(); } }); } }
private Semaphore terminations = new Semaphore(0); protected void beforeExecute (final Thread thread, final Runnable job) { if (terminations.tryAcquire()) { /* Replace this item in the queue so it may be executed by another * thread */ queue.add(job); thread.setUncaughtExceptionHandler( new ShutdownHandler(thread.getUncaughtExceptionHandler()) ); /* Throwing a runtime exception is the only way to prematurely * cause a worker thread from the TheadPoolExecutor to exit. */ throw new ShutdownException("Terminating thread"); } } public void setCorePoolSize (final int size) { int delta = getActiveCount() - size; super.setCorePoolSize(size); if (delta > 0) { terminations.release(delta); } }
PartitionedUnorderedExecutor executor = new PartitionedUnorderedExecutor(queue, service, jobCount); final Semaphore jobSemaphore = new Semaphore(0); final Semaphore testSemaphore = new Semaphore(0); final AtomicInteger interrupted = new AtomicInteger(); testSemaphore.release(); try { jobSemaphore.acquire(); } catch (InterruptedException e) { interrupted.incrementAndGet();
PartitionedOrderedExecutor executor = new PartitionedOrderedExecutor(queue, service); final Semaphore jobSemaphore = new Semaphore(0); final Semaphore testSemaphore = new Semaphore(0); testSemaphore.release(); jobSemaphore.acquireUninterruptibly(); }); assertThat(executor.isTerminated(), is(false)); jobSemaphore.release(); assertThat(executor.awaitTermination(2, MINUTES), is(true)); assertThat(executor.isShutdown(), is(true));