From 293fa0e496f9b25061812849fc59db6bcfd442ca Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Mon, 22 Jun 2020 12:53:12 -0700 Subject: [PATCH] Name threads in completion service Naming the threads makes it easier to read the program state in jstack. --- tasks/src/main/scala/sbt/CompletionService.scala | 13 ++++++++++--- .../main/scala/sbt/ConcurrentRestrictions.scala | 15 ++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/tasks/src/main/scala/sbt/CompletionService.scala b/tasks/src/main/scala/sbt/CompletionService.scala index 54d619d09..347c514a6 100644 --- a/tasks/src/main/scala/sbt/CompletionService.scala +++ b/tasks/src/main/scala/sbt/CompletionService.scala @@ -23,18 +23,25 @@ trait CompletionService[A, R] { def take(): R } +import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.{ Callable, - CompletionService => JCompletionService, Executor, - Executors, ExecutorCompletionService, + Executors, RejectedExecutionException, + CompletionService => JCompletionService } object CompletionService { + val poolID = new AtomicInteger(1) def apply[A, T](poolSize: Int): (CompletionService[A, T], () => Unit) = { - val pool = Executors.newFixedThreadPool(poolSize) + val i = new AtomicInteger(1) + val id = poolID.getAndIncrement() + val pool = Executors.newFixedThreadPool( + poolSize, + (r: Runnable) => new Thread(r, s"sbt-completion-thread-$id-${i.getAndIncrement}") + ) (apply[A, T](pool), () => { pool.shutdownNow(); () }) } def apply[A, T](x: Executor): CompletionService[A, T] = diff --git a/tasks/src/main/scala/sbt/ConcurrentRestrictions.scala b/tasks/src/main/scala/sbt/ConcurrentRestrictions.scala index 50fba84dd..d289c453c 100644 --- a/tasks/src/main/scala/sbt/ConcurrentRestrictions.scala +++ b/tasks/src/main/scala/sbt/ConcurrentRestrictions.scala @@ -7,6 +7,8 @@ package sbt +import java.util.concurrent.atomic.AtomicInteger + import sbt.internal.util.AttributeKey /** @@ -119,6 +121,8 @@ object ConcurrentRestrictions { private[this] def merge[A, B](m: Map[A, B], n: Map[A, B])(f: (B, B) => B): Map[A, B] = n.foldLeft(m) { case (acc, (a, b)) => update(acc, a, b)(f) } + private[this] val poolID = new AtomicInteger(1) + /** * Constructs a CompletionService suitable for backing task execution based on the provided restrictions on concurrent task execution. * @return a pair, with _1 being the CompletionService and _2 a function to shutdown the service. @@ -129,11 +133,12 @@ object ConcurrentRestrictions { tags: ConcurrentRestrictions[A], warn: String => Unit ): (CompletionService[A, R], () => Unit) = { - val pool = Executors.newCachedThreadPool() - (completionService[A, R](pool, tags, warn), () => { - pool.shutdownNow() - () - }) + val id = poolID.getAndIncrement + val i = new AtomicInteger(1) + val pool = Executors.newCachedThreadPool { r => + new Thread(r, s"sbt-completion-service-pool-$id-${i.getAndIncrement()}") + } + (completionService[A, R](pool, tags, warn), () => { pool.shutdownNow(); () }) } def completionService[A, R](