In my application I have 4 distinct processes, which run permanently with some small pauses.
The current version of code executes each process in a separate old-school thread:
Thread nlpAnalyzer = new Thread(() -> {
// infine lop for auto restore in case of crash
//noinspection InfiniteLoopStatement
while (true) {
try {
// this method should run permanently, pauses implemented internally
NLPAnalyzer.analyzeNLP(dbCollection);
} catch (Exception e) {
e.printStackTrace();
}
}
});
nlpAnalyzer.setName("im_nlpAnalyzer");
nlpAnalyzer.start();
Now I would like to refactor this code with use of ExecutorService
. In order to do that I can use at least two approaches:
newFixedThreadPool(numOfProc)
;numOfProc * newSingleThreadExecutor()
.
My questions:
Is there any reason why I should prefer one option over another?
What is more accepted to generate a thread pool with X threads or generate X
newSingleThreadExecutor
s?Pro et contra of each of the approach?