Skip to content

Commit 5d6193f

Browse files
committed
Added change to terminate idle threads in the executorPool to free up memory.
1 parent fa9583e commit 5d6193f

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/RegionalAccessBoundaryManager.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,24 @@ final class RegionalAccessBoundaryManager {
8686
// on concurrent refresh tasks, while threadCount provides unique names
8787
// for easier debugging.
8888
private static final AtomicInteger threadCount = new AtomicInteger(0);
89-
private static final ExecutorService EXECUTOR =
90-
Executors.newFixedThreadPool(
91-
5,
92-
r -> {
93-
Thread t = new Thread(r, "RAB-refresh-" + threadCount.getAndIncrement());
94-
t.setDaemon(true);
95-
return t;
96-
});
89+
private static final ExecutorService EXECUTOR;
90+
91+
static {
92+
java.util.concurrent.ThreadPoolExecutor executor =
93+
new java.util.concurrent.ThreadPoolExecutor(
94+
5, // corePoolSize: threads to keep alive
95+
5, // maximumPoolSize: max threads allowed
96+
1, // keepAliveTime: time to wait before terminating idle threads
97+
java.util.concurrent.TimeUnit.HOURS, // unit for keepAliveTime
98+
new java.util.concurrent.LinkedBlockingQueue<>(), // work queue
99+
r -> {
100+
Thread t = new Thread(r, "RAB-refresh-" + threadCount.getAndIncrement());
101+
t.setDaemon(true);
102+
return t;
103+
});
104+
executor.allowCoreThreadTimeOut(true);
105+
EXECUTOR = executor;
106+
}
97107

98108
private final transient Clock clock;
99109
private final int maxRetryElapsedTimeMillis;

0 commit comments

Comments
 (0)