Skip to content

Commit 351e05a

Browse files
Alex Millerstuarthalloway
authored andcommitted
#378 set thread names on agent thread pools
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
1 parent 038e4a8 commit 351e05a

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

src/jvm/clojure/lang/Agent.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
package clojure.lang;
1414

15-
import java.util.concurrent.*;
15+
import java.util.concurrent.ExecutorService;
16+
import java.util.concurrent.Executors;
17+
import java.util.concurrent.ThreadFactory;
18+
import java.util.concurrent.atomic.AtomicLong;
1619
import java.util.concurrent.atomic.AtomicReference;
17-
import java.util.Map;
1820

1921
public class Agent extends ARef {
2022

@@ -34,18 +36,33 @@ public ActionQueue( IPersistentStack q, Throwable error )
3436
static final Keyword FAIL = Keyword.intern(null, "fail");
3537

3638
volatile Object state;
37-
AtomicReference<ActionQueue> aq = new AtomicReference(ActionQueue.EMPTY);
39+
AtomicReference<ActionQueue> aq = new AtomicReference<ActionQueue>(ActionQueue.EMPTY);
3840

3941
volatile Keyword errorMode = CONTINUE;
4042
volatile IFn errorHandler = null;
4143

44+
final private static AtomicLong sendThreadPoolCounter = new AtomicLong(0);
45+
46+
final private static AtomicLong sendOffThreadPoolCounter = new AtomicLong(0);
47+
4248
final public static ExecutorService pooledExecutor =
43-
Executors.newFixedThreadPool(2 + Runtime.getRuntime().availableProcessors());
49+
Executors.newFixedThreadPool(2 + Runtime.getRuntime().availableProcessors(),
50+
createThreadFactory("clojure-agent-send-pool-%d", sendThreadPoolCounter));
4451

45-
final public static ExecutorService soloExecutor = Executors.newCachedThreadPool();
52+
final public static ExecutorService soloExecutor = Executors.newCachedThreadPool(
53+
createThreadFactory("clojure-agent-send-off-pool-%d", sendOffThreadPoolCounter));
4654

4755
final static ThreadLocal<IPersistentVector> nested = new ThreadLocal<IPersistentVector>();
4856

57+
private static ThreadFactory createThreadFactory(final String format, final AtomicLong threadPoolCounter) {
58+
return new ThreadFactory() {
59+
@Override public Thread newThread(Runnable runnable) {
60+
Thread thread = new Thread(runnable);
61+
thread.setName(String.format(format, threadPoolCounter.getAndIncrement()));
62+
return thread;
63+
}
64+
};
65+
}
4966

5067
public static void shutdown(){
5168
soloExecutor.shutdown();

0 commit comments

Comments
 (0)