Skip to content

Commit

Permalink
Merge pull request #80 from aubi/virtualthreads-fix-int-overflow-queu…
Browse files Browse the repository at this point in the history
…e-size
  • Loading branch information
arjantijms committed Mar 4, 2024
2 parents 86c784c + af706af commit 3703216
Showing 1 changed file with 7 additions and 2 deletions.
Expand Up @@ -83,7 +83,12 @@ public VirtualThreadsManagedExecutorService(String name,
if (maxParallelTasks > 0) {
parallelTasksSemaphore = new Semaphore(maxParallelTasks, true);
if (queueCapacity > 0) {
queuedTasksSemaphore = new Semaphore(queueCapacity + maxParallelTasks, true);
int virtualCapacity = queueCapacity + maxParallelTasks;
if (virtualCapacity <= 0) {
// int overflow; queue capacity is often MAX_VALUE
virtualCapacity = Integer.MAX_VALUE;
}
queuedTasksSemaphore = new Semaphore(virtualCapacity, true);
}
}
executor = Executors.newThreadPerTaskExecutor(getManagedThreadFactory());
Expand Down Expand Up @@ -111,7 +116,7 @@ protected void executeManagedFutureTask(ManagedFutureTask<?> task) {
getThreadPoolExecutor().execute(task);
runningFutures.add(task);
} else {
throw new RejectedExecutionException("Too many tasks submitted (maxParallelTasks=" + maxParallelTasks + ", queueCapacity=" + queueCapacity);
throw new RejectedExecutionException("Too many tasks submitted (available = " + (queuedTasksSemaphore == null ? "UNUSED" : queuedTasksSemaphore.availablePermits()) + ", maxParallelTasks = " + maxParallelTasks + ", queueCapacity = " + queueCapacity);
}
}

Expand Down

0 comments on commit 3703216

Please sign in to comment.