Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it means "corePoolSize", param of newScheduledThreadPool() method?

i dont have clear what does it means the "corePoolSize" parameter of the newScheduledThreadPool() method from class java.util.concurrent.Executors.

What happen if i put a higher number value and what happen if i put a lower number value?

// corePoolSize = 1;
java.util.concurrent.Executors.newScheduledThreadPool(corePoolSize);

or

// corePoolSize = 5;
java.util.concurrent.Executors.newScheduledThreadPool(corePoolSize);

What is the correct way to define that value?

like image 831
Ninja Coding Avatar asked Mar 13 '23 21:03

Ninja Coding


1 Answers

It is explained in details in the javadoc of ThreadPoolExecutor - extract:

When a new task is submitted in method execute(Runnable), and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle. If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full.

So it defines if threads should be created or not depending on the state of the executor.

In the case of a ScheduledExecutorService, if you don't plan to have more than one task running at a given time, a corePoolSize of 1 is probably more efficient. And it won't prevent more threads to be created if required.

like image 59
assylias Avatar answered Mar 16 '23 09:03

assylias