给定以下课程:
public class Poller implements Runnable {
public static final int CORE_POOL_SIZE = 4;
public boolean running;
public ScheduledExecutorService ses;
public void startPolling() {
this.ses = Executors.newScheduledThreadPool(CORE_POOL_SIZE);
this.ses.scheduleAtFixedRate(this, 0, 1, TimeUnit.SECONDS);
}
public void run() {
running = true;
// ... Do something ...
running = false;
}
}
核心线程池大小为 4 ,ScheduledExecutorService
但会创建多个轮询线程吗?既然this
被传递到scheduleAtFixedRate
,这是否意味着只会有一个线程 - 或者在幕后发生更复杂的事情?
还有 2 个奖励问题:-
- 应该
running
是static
? - 是
CORE_POOL_SIZE
多余的吗?