title: How to optimize number of threads to speed up processing
body: <p><strong>I have read many similar <a href="https://stackoverflow.com/questions/10116502/java-threading-optimization-at-100-cpu-usage">questions</a> . However I was not quite satisfied with answers.</strong> </p>
  
  <p>I would like to build an algorithm that would adjust the number of threads depending on the <strong>average speed</strong>.   </p>
  
  <p>Let's say as I introduce a new thread, the average speed of task execution increases , it means that the new thread is good. Then the algorithm should  try to add another thread ... until the optimal number of threads is achieved .......</p>
  
  <p>Also the algorithm should be keeping track of the average speed. If at some point the average speed goes down significantly, let's say by 10 % (for any reason e.g. i open a different application or whatever) , then the algorithm should terminate one thread and see if the speed goes up ...  </p>
  
  <p>Maybe such an API exists. Please, give me any directions or any code example how I could implement such an algorithm</p>
  
  <p>Thank You !</p>