user1950349
user1950349

Reputation: 5146

understanding of thread pool size in executor service

I have a thread pool size of 3 as shown below and in the constructor I am using same thread pool to start two different threads:

Below is my code:

private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);

private Data() {
  executorService.submit(new Poller());
  executorService.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
      for (Entry<Long, byte[]> entry : retryHolder.asMap().entrySet()) {
        execute(entry.getKey(), entry.getValue());
      }
    }
  }, 0, 1, TimeUnit.SECONDS);
}

My question is do I need thread pool of size 3 or I can just live with 2 here? Since I know it will use only two thread as per my above code so apparently I might be wasting the other thread?

Upvotes: 1

Views: 1902

Answers (1)

Rick Ridley
Rick Ridley

Reputation: 583

According to the documentation of scheduleAtFixedRate:

If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.

So you will be fine with using only 2 threads in your pool, but there is likely only a minimal (if there is any at all) performance/overhead downside with using 3 threads in your pool.

Upvotes: 1

Related Questions