title: How many threads should I use in my Java program?
body: <p>I recently inherited a small Java program that takes information from a large database, does some processing and produces a detailed image regarding the information. The original author wrote the code using a single thread, then later modified it to allow it to use multiple threads. </p>
  
  <p>In the code he defines a constant;</p>
  
  <pre><code>//  number of threads
  public static final int THREADS =  Runtime.getRuntime().availableProcessors();
  </code></pre>
  
  <p>Which then sets the number of threads that are used to create the image.</p>
  
  <p>I understand his reasoning that the number of threads cannot be greater than the number of available processors, so set it the the amount to get the full potential out of the processor(s). Is this correct? or is there a better way to utilize the full potential of the processor(s)?</p>
  
  <p>EDIT: To give some more clarification, The specific algorithm that is being threaded scales to the resolution of the picture being created, (1 thread per pixel). That is obviously not the best solution though. The work that this algorithm does is what takes all the time, and is wholly mathematical operations, there are no locks or other factors that will cause any given thread to sleep. I just want to maximize the programs CPU utilization to decrease the time to completion.</p>