3333import java .util .concurrent .ScheduledExecutorService ;
3434import java .util .concurrent .ScheduledThreadPoolExecutor ;
3535import java .util .concurrent .ThreadFactory ;
36+ import java .util .concurrent .atomic .AtomicInteger ;
3637
3738/**
3839 * InstantiatingChannelProvider is an ExecutorProvider which constructs a new
4243public 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