On the subject of Java code optimisation: I’m sure we’re all familiar with the concept of thread pooling — typically used together with the Executor framework. Configuring your thread pools can be tricky: choose a small value and you’re not making usage of your server power, on the other hand, choose a value large enough and the JVM will spend too much time switching thread contexts and doesn’t get to actually do the work. Ideally you want the size of your thread pool very close to the number of cores/processors in your system.
Luckily this can be done with the help of the
Runtime class: this class offers a method called
availableProcessors which according to the JDK “returns the number of processors available to the Java virtual machine”. Note that it seems you cannot find out the number of processors to the system but only the number of processors available to the JVM — which is still good enough, because if the OS has been configured to only give you use of half of the processors, even if you find out the real value you can’t access them. So with this in mind, the next time you configure your thread pool, worth inspecting this property — depending on what else goes in your program you might want to consider setting your threadpool to
Runtime.availableProcessors() -1 (allow an extra core/processor to the main app thread for instace)!
More from my site