3

How to use an ExecutorService in a way that a central Thread Pool is created for the application at the application level whose pool size will be set according to the number of threads available with the CPU at that time and then the different functionalities of the application use thread from this central pool as per their requirement.

Man
  • 43
  • 1
  • 8
  • Use some custom wrapper around `ExecutorService` that is a singleton (using dependency injection maybe)? –  Nov 24 '16 at 05:42
  • 1
    You can use `Runtime.getRuntime().availableProcessors();` to get the processor count though I wouldn't recommend using this. Its better to profile your code to find the optimum number of threads. – Pravin Sonawane Nov 24 '16 at 05:45
  • If you re looking for a way to calculate how many threads your environment can support .. here are some ..http://stackoverflow.com/questions/763579/how-many-threads-can-a-java-vm-support – Sagar Nov 24 '16 at 05:49
  • This question may be useful for you : http://stackoverflow.com/questions/1980832/java-how-to-scale-threads-according-to-cpu-cores/36723383#36723383 – Ravindra babu Nov 30 '16 at 15:07
  • There is also issue around when finding the current JVM thread count:- Need to find standard Threshold value. If we take Happy Case : If JVM thread count is less than threshold value,then we will get thread from pool and process it. No issue on this case. But in Sad Case : If JVM thread count is greater than threshold value,what to do here? So we need to wait till jvm thread is cool down. If we are waiting for thread , it is too expensive in terms of CPU utilization . If we continue to wait, then process of record is accumulated and again it will be a issue in terms of performance. – Man Nov 30 '16 at 23:52
  • ForkJoinPool seems to be suitable for your need. By default, it is created constructed with given target parallelism (number of processors). – ak1 Dec 19 '16 at 05:27

2 Answers2

2

As of Java 8, I suggest you use ForkJoinPool.commonPool(). This is the only global thread pool that base Java provides.

Before Java 8, you either keep your own thread pool(s) or use your framework's shareable thread pool(s).

acelent
  • 7,965
  • 21
  • 39
1

Bellow is my view:

  • a central Thread Pool?

    Maybe, it's say the Singleton Pattern in Design Pattern, I think it can solve your problem;

  • set according to the number of threads available with the CPU?

    The size of the thread pool isn't exact. In practice, the size depends on the tasks' type be executed by the thread pool. For example, the size can be Runtime.getRuntime().availableProcessors() + 1 if the tasks are CPU intensive, or be Runtime.getRuntime().availableProcessors() * 2 if the tasks are I/O intensive.But these are only basic principles, you should determine the appropriate size by testing your application with some guidelines(such as Little's_law);

  • my suggests:

    In practice, I seldom submit all tasks to only one central threal pool, maybe should group your tasks by type, submit them to the different thread pool, this will be convenient to monitor or tuning the thread pool later.

Hope to help you.

haolin
  • 216
  • 1
  • 5
  • Thanks Haolin! There is also issue around when finding the current JVM thread count:- Need to find standard Threshold value Happy Case : If JVM thread count is less than threshold value,then we will get thread from pool and process it. No issue on this case Sad Case : If JVM thread count is greater than threshold value,what to do here? So we need to wait till jvm thread is cool down. If we are waiting for thread , it is too expensive in terms of CPU utilization . If we continue to wait, then process of record is accumulated and again it will be a issue in terms of performance. – Man Nov 30 '16 at 23:52