在我的应用程序中,我有4个不同的进程,它们会一直运行,但会有一些小的暂停。
当前版本的代码使用单独的传统线程来执行每个进程:
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();
现在我想使用
ExecutorService
重构这段代码。为了做到这一点,我可以采用至少两种方法:
newFixedThreadPool(numOfProc)
;numOfProc * newSingleThreadExecutor()
。
- 有没有理由我应该更喜欢其中一种选择?
- 生成一个包含X个线程的线程池或者生成X个
newSingleThreadExecutor
哪个更被接受? - 每种方法的优缺点是什么?