use of com.hubspot.singularity.async.ExecutorAndQueue in project Singularity by HubSpot.
the class SingularityManagedThreadPoolFactory method get.
public synchronized ExecutorAndQueue get(String name, int maxSize, int queueSize, boolean blockWhenFull) {
checkState(!stopped.get(), "already stopped");
LinkedBlockingQueue<Runnable> queue = blockWhenFull ? new ThreadPoolQueue(queueSize) : new LinkedBlockingQueue<>(queueSize);
ExecutorService service = new ThreadPoolExecutor(maxSize, maxSize, 0L, TimeUnit.MILLISECONDS, queue, new ThreadFactoryBuilder().setNameFormat(name + "-%d").build());
executorPools.add(service);
return new ExecutorAndQueue(service, queue, queueSize);
}
use of com.hubspot.singularity.async.ExecutorAndQueue in project Singularity by HubSpot.
the class SingularityBlockingThreadPoolTest method testBoundedQueueBlocksWhenFullForCompletableFutures.
@Test
public void testBoundedQueueBlocksWhenFullForCompletableFutures() {
SingularityManagedThreadPoolFactory threadPoolFactory = new SingularityManagedThreadPoolFactory(new SingularityConfiguration());
Assertions.assertThrows(RejectedExecutionException.class, () -> {
ExecutorAndQueue executorAndQueue = threadPoolFactory.get("test", 2, 5, false);
IntStream.range(0, 10).forEach(i -> CompletableFuture.runAsync(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
// didn't see that...
}
}, executorAndQueue.getExecutorService()));
});
Assertions.assertDoesNotThrow(() -> {
ExecutorAndQueue executorAndQueue = threadPoolFactory.get("test", 2, 5, true);
IntStream.range(0, 10).forEach(i -> CompletableFuture.runAsync(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
// didn't see that...
}
}, executorAndQueue.getExecutorService()));
});
}
use of com.hubspot.singularity.async.ExecutorAndQueue in project Singularity by HubSpot.
the class SingularityBlockingThreadPoolTest method testBoundedQueueBlocksWhenFull.
@Test
public void testBoundedQueueBlocksWhenFull() {
SingularityManagedThreadPoolFactory threadPoolFactory = new SingularityManagedThreadPoolFactory(new SingularityConfiguration());
Assertions.assertThrows(RejectedExecutionException.class, () -> {
ExecutorAndQueue executorAndQueue = threadPoolFactory.get("test", 2, 5, false);
IntStream.range(0, 10).forEach(i -> executorAndQueue.getExecutorService().submit(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
// didn't see that...
}
}));
});
Assertions.assertDoesNotThrow(() -> {
ExecutorAndQueue executorAndQueue = threadPoolFactory.get("test", 2, 5, true);
IntStream.range(0, 10).forEach(i -> executorAndQueue.getExecutorService().submit(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
// didn't see that...
}
}));
});
}
Aggregations