Search in sources :

Example 1 with ExecutorAndQueue

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);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ExecutorAndQueue(com.hubspot.singularity.async.ExecutorAndQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 2 with ExecutorAndQueue

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()));
    });
}
Also used : SingularityManagedThreadPoolFactory(com.hubspot.singularity.SingularityManagedThreadPoolFactory) ExecutorAndQueue(com.hubspot.singularity.async.ExecutorAndQueue) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) Test(org.junit.jupiter.api.Test)

Example 3 with ExecutorAndQueue

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...
            }
        }));
    });
}
Also used : SingularityManagedThreadPoolFactory(com.hubspot.singularity.SingularityManagedThreadPoolFactory) ExecutorAndQueue(com.hubspot.singularity.async.ExecutorAndQueue) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) Test(org.junit.jupiter.api.Test)

Aggregations

ExecutorAndQueue (com.hubspot.singularity.async.ExecutorAndQueue)3 SingularityManagedThreadPoolFactory (com.hubspot.singularity.SingularityManagedThreadPoolFactory)2 SingularityConfiguration (com.hubspot.singularity.config.SingularityConfiguration)2 Test (org.junit.jupiter.api.Test)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ExecutorService (java.util.concurrent.ExecutorService)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1