Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit d4907e6

Browse files
j256garrettjonesgoogle
authored andcommitted
Added thread factory so we can set thread-name on (for example) pub-sub worker threads (#536)
1 parent a4818a8 commit d4907e6

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

gax/src/main/java/com/google/api/gax/core/InstantiatingExecutorProvider.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.concurrent.ScheduledExecutorService;
3434
import java.util.concurrent.ScheduledThreadPoolExecutor;
3535
import java.util.concurrent.ThreadFactory;
36+
import java.util.concurrent.atomic.AtomicInteger;
3637

3738
/**
3839
* InstantiatingChannelProvider is an ExecutorProvider which constructs a new
@@ -42,22 +43,26 @@
4243
public abstract class InstantiatingExecutorProvider implements ExecutorProvider {
4344
// The number of threads to use with the default executor.
4445
private static final int DEFAULT_EXECUTOR_THREADS = 4;
46+
// Thread factory to use to create our worker threads
47+
private static final ThreadFactory DEFAULT_THREAD_FACTORY =
48+
new ThreadFactory() {
49+
private final AtomicInteger threadCount = new AtomicInteger();
50+
51+
@Override
52+
public Thread newThread(Runnable runnable) {
53+
Thread thread = new Thread(runnable);
54+
thread.setName("Gax-" + threadCount.incrementAndGet());
55+
thread.setDaemon(true);
56+
return thread;
57+
}
58+
};
4559

4660
// Package-private constructor prevents others from subclassing.
4761
InstantiatingExecutorProvider() {}
4862

4963
@Override
5064
public ScheduledExecutorService getExecutor() {
51-
return new ScheduledThreadPoolExecutor(
52-
getExecutorThreadCount(),
53-
new ThreadFactory() {
54-
@Override
55-
public Thread newThread(Runnable r) {
56-
Thread t = new Thread(r);
57-
t.setDaemon(true);
58-
return t;
59-
}
60-
});
65+
return new ScheduledThreadPoolExecutor(getExecutorThreadCount(), getThreadFactory());
6166
}
6267

6368
@Override
@@ -68,13 +73,17 @@ public boolean shouldAutoClose() {
6873
/** The number of threads used by the executor created by this ExecutorProvider. */
6974
public abstract int getExecutorThreadCount();
7075

76+
/** Return a thread-factory to create gax processing threads so we can name them appropriately */
77+
public abstract ThreadFactory getThreadFactory();
78+
7179
public Builder toBuilder() {
7280
return new AutoValue_InstantiatingExecutorProvider.Builder(this);
7381
}
7482

7583
public static Builder newBuilder() {
7684
return new AutoValue_InstantiatingExecutorProvider.Builder()
77-
.setExecutorThreadCount(DEFAULT_EXECUTOR_THREADS);
85+
.setExecutorThreadCount(DEFAULT_EXECUTOR_THREADS)
86+
.setThreadFactory(DEFAULT_THREAD_FACTORY);
7887
}
7988

8089
@AutoValue.Builder
@@ -83,6 +92,10 @@ public abstract static class Builder {
8392

8493
public abstract int getExecutorThreadCount();
8594

95+
public abstract Builder setThreadFactory(ThreadFactory value);
96+
97+
public abstract ThreadFactory getThreadFactory();
98+
8699
public abstract InstantiatingExecutorProvider build();
87100
}
88101
}

0 commit comments

Comments
 (0)