@Test void shouldRejectTasksOnceQueueIsFull() { final Task task1 = new Task(); final Task task2 = new Task(); final Task task3 = new Task(); final Task task4 = new Task(); final ExecutorService executorService = newCachedThreadPool(1, 2); executorService.execute(task1); executorService.execute(task2); executorService.execute(task3); assertThatThrownBy(() -> executorService.execute(task4)) .isInstanceOf(RejectedExecutionException.class); }
public ExecutorService newCachedThreadPool( final String name, final int maxThreads, final int maxQueueSize, final ThreadFactory threadFactory) { final ThreadPoolExecutor executor = new ThreadPoolExecutor( maxThreads, maxThreads, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(maxQueueSize), threadFactory); executor.allowCoreThreadTimeOut(true); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_queue_size", "Current size of the executor task queue", () -> executor.getQueue().size()); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_thread_pool_size", "Current number of threads in the executor thread pool", executor::getPoolSize); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_thread_active_count", "Current number of threads executing tasks for this executor", executor::getActiveCount); return executor; }
MetricTrackingExecutorFactory { public ExecutorService newCachedThreadPool( final String name, final int maxThreads, final int maxQueueSize, final ThreadFactory threadFactory) { final ThreadPoolExecutor executor = new ThreadPoolExecutor( maxThreads, maxThreads, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(maxQueueSize), threadFactory); executor.allowCoreThreadTimeOut(true); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_queue_size", "Current size of the executor task queue", () -> executor.getQueue().size()); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_thread_pool_size", "Current number of threads in the executor thread pool", executor::getPoolSize); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_thread_active_count", "Current number of threads executing tasks for this executor", executor::getActiveCount); return executor; } }
MetricTrackingExecutorFactory { public ExecutorService newCachedThreadPool( final String name, final int maxThreads, final int maxQueueSize, final ThreadFactory threadFactory) { final ThreadPoolExecutor executor = new ThreadPoolExecutor( maxThreads, maxThreads, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(maxQueueSize), threadFactory); executor.allowCoreThreadTimeOut(true); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_queue_size", "Current size of the executor task queue", () -> executor.getQueue().size()); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_thread_pool_size", "Current number of threads in the executor thread pool", executor::getPoolSize); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_thread_active_count", "Current number of threads executing tasks for this executor", executor::getActiveCount); return executor; } MetricTrackingExecutorFactory(final MetricsSystem metricsSystem); }
MetricTrackingExecutorFactory { public ExecutorService newCachedThreadPool( final String name, final int maxThreads, final int maxQueueSize, final ThreadFactory threadFactory) { final ThreadPoolExecutor executor = new ThreadPoolExecutor( maxThreads, maxThreads, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(maxQueueSize), threadFactory); executor.allowCoreThreadTimeOut(true); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_queue_size", "Current size of the executor task queue", () -> executor.getQueue().size()); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_thread_pool_size", "Current number of threads in the executor thread pool", executor::getPoolSize); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_thread_active_count", "Current number of threads executing tasks for this executor", executor::getActiveCount); return executor; } MetricTrackingExecutorFactory(final MetricsSystem metricsSystem); ExecutorService newCachedThreadPool(
      final String name,
      final int maxThreads,
      final int maxQueueSize,
      final ThreadFactory threadFactory); }
MetricTrackingExecutorFactory { public ExecutorService newCachedThreadPool( final String name, final int maxThreads, final int maxQueueSize, final ThreadFactory threadFactory) { final ThreadPoolExecutor executor = new ThreadPoolExecutor( maxThreads, maxThreads, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(maxQueueSize), threadFactory); executor.allowCoreThreadTimeOut(true); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_queue_size", "Current size of the executor task queue", () -> executor.getQueue().size()); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_thread_pool_size", "Current number of threads in the executor thread pool", executor::getPoolSize); metricsSystem.createIntegerGauge( TekuMetricCategory.EXECUTOR, name + "_thread_active_count", "Current number of threads executing tasks for this executor", executor::getActiveCount); return executor; } MetricTrackingExecutorFactory(final MetricsSystem metricsSystem); ExecutorService newCachedThreadPool(
      final String name,
      final int maxThreads,
      final int maxQueueSize,
      final ThreadFactory threadFactory);  }