我对于executor何时创建新线程感到困惑。我的理解是:对于cachedthreadpool,根据任务的提交情况创建新线程。对于fixedthreadpool和singlethread,无论任务是否被提交,线程都会提前创建。请问我的理解是否正确?
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new LiftOff());
ExecutorService exec = Executors.newFixedThreadPool(3); // Are the three threads created
// after this line executes?
exec.execute(new LiftOff());
ExecutorService exec = Executors.newSingleThreadExecutor(); // Is the single thread created
// after this line executes?
exec.execute(new LiftOff());
Executors
的代码。 - Oliver CharlesworthThreadFactory
的变体,您可以传入自己的实现,在请求线程时记录它。 - pvg