Refine search
public void addCall(final FSTRunnable toRun) throws InterruptedException { gateway.acquire(); if (jobs[curIdx] == null) { jobs[curIdx] = toRun; } else { jobs[curIdx].sem.acquire(); jobs[curIdx].sem.release(); jobs[curIdx] = toRun; } toRun.sem = sems[curIdx]; toRun.sem.acquire(); OrderedRunnable ord = orderedRunnableCache[curIdx]; ord.toRun = toRun; curIdx = (curIdx + 1) % threads; orderedPool.execute(ord); pool.execute(toRun); }
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 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 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(); } }
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(); } }); } }
@Test public void testRunningJobsAreInterruptedAfterShutdownNow() throws InterruptedException { final int jobCount = 4; ExecutorService worker = Executors.newCachedThreadPool(); try { PartitionedScheduledExecutor executor = new PartitionedScheduledExecutor(scheduler, worker); final Semaphore jobSemaphore = new Semaphore(0); final Semaphore testSemaphore = new Semaphore(0); final AtomicInteger interrupted = new AtomicInteger(); for (int i = 0; i < jobCount; i++) { executor.submit(() -> { testSemaphore.release(); try { jobSemaphore.acquire(); } catch (InterruptedException e) { interrupted.incrementAndGet(); } }); } testSemaphore.acquireUninterruptibly(jobCount); 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(jobCount)); } finally { worker.shutdown(); } }
private AtomicInteger render(Mustache test, ExecutorService es) throws InterruptedException { final AtomicInteger total = new AtomicInteger(); final Semaphore semaphore = new Semaphore(100); for (int i = 0; i < 100000; i++) { semaphore.acquire(); es.submit(() -> { try { TestObject testObject = new TestObject(r.nextInt(), r.nextInt(), r.nextInt()); StringWriter sw = new StringWriter(); test.execute(sw, testObject).close(); if (!render(testObject).equals(sw.toString())) { total.incrementAndGet(); } } catch (IOException e) { // Can't fail e.printStackTrace(); System.exit(1); } finally { semaphore.release(); } }); } // Wait for them all to complete semaphore.acquire(100); return total; }
public void addCall(final FSTRunnable toRun) throws InterruptedException { gateway.acquire(); if (jobs[curIdx] == null) { jobs[curIdx] = toRun; } else { jobs[curIdx].sem.acquire(); jobs[curIdx].sem.release(); jobs[curIdx] = toRun; } toRun.sem = sems[curIdx]; toRun.sem.acquire(); OrderedRunnable ord = orderedRunnableCache[curIdx]; ord.toRun = toRun; curIdx = (curIdx + 1) % threads; orderedPool.execute(ord); pool.execute(toRun); }
/** * Capture the current image with the size as it is displayed and retrieve it as Bitmap. * * @return current output as Bitmap * @throws InterruptedException */ public Bitmap capture() throws InterruptedException { final Semaphore waiter = new Semaphore(0); final int width = surfaceView.getMeasuredWidth(); final int height = surfaceView.getMeasuredHeight(); // Take picture on OpenGL thread final Bitmap resultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); gpuImage.runOnGLThread(new Runnable() { @Override public void run() { GPUImageNativeLibrary.adjustBitmap(resultBitmap); waiter.release(); } }); requestRender(); waiter.acquire(); return resultBitmap; }
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();