Skip to content

Commit 62c31fb

Browse files
committed
Switch to an internal daemon threads based executor that also closes threads when not used that much
1 parent 24af885 commit 62c31fb

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/main/java/engineering/swat/watch/impl/jdk/JDKPoller.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@
4646
import java.util.concurrent.ExecutionException;
4747
import java.util.concurrent.ExecutorService;
4848
import java.util.concurrent.Executors;
49+
import java.util.concurrent.LinkedBlockingQueue;
50+
import java.util.concurrent.ThreadFactory;
51+
import java.util.concurrent.ThreadPoolExecutor;
4952
import java.util.concurrent.TimeUnit;
53+
import java.util.concurrent.atomic.AtomicInteger;
5054
import java.util.function.Consumer;
5155

5256
import org.apache.logging.log4j.Level;
@@ -67,12 +71,23 @@ private JDKPoller() {}
6771
private static final Logger logger = LogManager.getLogger();
6872
private static final Map<WatchKey, Consumer<List<WatchEvent<?>>>> watchers = new ConcurrentHashMap<>();
6973
private static final WatchService service;
70-
private static final int nCores = Runtime.getRuntime().availableProcessors();
7174
/**
7275
* We have to be a bit careful with registering too many paths in parallel
7376
* Linux can be thrown into a deadlock if you try to start 1000 threads and then do a register at the same time.
7477
*/
75-
private static final ExecutorService registerPool = Executors.newFixedThreadPool(nCores);
78+
private static final ExecutorService registerPool = new ThreadPoolExecutor(
79+
0, Runtime.getRuntime().availableProcessors(),
80+
10, TimeUnit.SECONDS,
81+
new LinkedBlockingQueue<>(), new ThreadFactory() {
82+
private final AtomicInteger id = new AtomicInteger(0);
83+
private final ThreadGroup group = new ThreadGroup("registry pool");
84+
@Override
85+
public Thread newThread(Runnable r) {
86+
var t = new Thread(group, r, "JavaWatch-registry-pool-" + id.incrementAndGet());
87+
t.setDaemon(true);
88+
return t;
89+
}
90+
});
7691

7792
static {
7893
try {

0 commit comments

Comments
 (0)