Skip to content

Commit ef16e20

Browse files
authored
Make thread pool has at leat one core thread, but allow for a timeout
1 parent c4a66e6 commit ef16e20

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/main/java/engineering/swat/watch/DaemonThreadPool.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ private DaemonThreadPool() {}
4646
* @return an exectutor with deamon threads and constainted to a certain maximum
4747
*/
4848
public static ExecutorService buildConstrainedCached(String name, int maxThreads) {
49-
return new ThreadPoolExecutor(0, maxThreads,
49+
if (maxThreads <= 0) {
50+
throw new IllegalArgumentException("maxThreads should be higher than 0");
51+
}
52+
var pool = new ThreadPoolExecutor(1, maxThreads,
5053
60, TimeUnit.SECONDS,
5154
new LinkedBlockingQueue<>(),
5255
buildFactory(name)
5356
);
57+
pool.allowCoreThreadTimeOut(true);
58+
return pool;
5459
}
5560

5661
private static ThreadFactory buildFactory(String name) {

0 commit comments

Comments
 (0)