1

I have a 3 instance of a class which is implementing runnable interface. I am instantiating my Executor class like below;

 executor = Executors.newScheduledThreadPool(2);<--- talking about this part
    executor.scheduleAtFixedRate(unassignedRunnable, 0, refreshTime, TimeUnit.SECONDS);
    executor.scheduleAtFixedRate(assignedToMeRunnable, 2, refreshTime, TimeUnit.SECONDS);
    executor.scheduleAtFixedRate(createTicketsFromFile, 3, refreshTime * 2, TimeUnit.SECONDS);

My question is, Does it make any difference, if I change thread pool count from 2 to 1 or 3 ? I tried and gained nothing almost. Can anyone explain the real use of thread pool count ? Maybe my tasks are lightweight ?

Ravi
  • 30,829
  • 42
  • 119
  • 173
Mert Serimer
  • 1,217
  • 2
  • 16
  • 38
  • 1
    Presumably your tasks are indeed lightweight. The parameter spawns new threads - if you have a task that runs every second and takes 900ms to run, then that thread will have 100ms spare. If you have another task that runs every second and takes 900ms to run then you thread pool will not be able to cope. There is no rule of thumb here - if your scheduled tasks are running within your required tolerance, then you do not need more threads. – Boris the Spider Jan 07 '18 at 15:26
  • @BoristheSpider i see. Thanks. So it is a little bit "try & see". – Mert Serimer Jan 07 '18 at 15:30
  • Yup. Benchmark, benchmark, benchmark. Then benchmark some more... – Boris the Spider Jan 07 '18 at 15:31
  • @BoristheSpider ok benchmark lover.. Thanks a lot. Please move your reply to answer part so i can choose as "answer". – Mert Serimer Jan 07 '18 at 15:37

3 Answers3

2

That is corePoolSize is number thread in pool .Available thread pick the eligible task and run in same thread.If there is no thread available though task is eligble for run will not execute as all threads are busy.In your case may be your tasks very short lived.To demo create corepool size one and submit long running task and after that submit a light task check the behavior then increase the corepoolsize to 2 and see the behavior.

gati sahu
  • 2,576
  • 2
  • 10
  • 16
2

You need to understand, it doesn't matter how many threads you are going to create, ultimately, threads would be executed upon number of available cores. Now, as per documentation, it is "the number of threads to keep in the pool, even if they are idle."

Can you tell me what is the real use of thread pool count

executor = Executors.newScheduledThreadPool(2);

Above line of code will create 2 threads in thread pool , but it doesn't mean all will be doing some work. But, on same time, the same thread can be used to perform some other task from thread pool, which was submitted.

So, it is better to understand your requirement before picking the total number of threads to be created. (I usually prefer the number, depending on the number of available cores count)

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • 1
    Available cores count is only relevant if tasks are CPU and not IO bound. If the scheduled tasks write audit logs to a file or whatever then having more threads that CPUs can make sense. – Boris the Spider Jan 07 '18 at 16:53
  • @BoristheSpider yes of course. If you wanted to suggest any statement, then please go ahead – Ravi Jan 07 '18 at 16:58
1

It depends on number of CPU cores of a machine, on which you are running your application. If you have more number of CPU cores, multiple threads can run in parallel and performance of overall system can be improved if your application is not IO Bound application.

CPU bound application will benefit with more number of cores & threads.

If you have 4 core CPU, you can configure the value as 4. If your machine has single CPU core, there won't be any benefit to change the pool size as 4.

Related SE questions:

Java: How to scale threads according to cpu cores?

Is multithreading faster than single thread?

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211