title: Spring - add a low priority multi threaded service (no impact to production performance)
body: <p>We have a Spring application, I want to add a <strong>service that will handle 10Ks IDs with multiple threads but will be as background process without impact production realtime</strong>.</p>
  
  <p>Service will update database and send external providers requests.</p>
  
  <p><strong>I don't want service to impact/effect production performance</strong>/timing, I want to execute operation on each ID in a low priority </p>
  
  <p>I read <a href="https://stackoverflow.com/questions/3198660/java-executors-how-can-i-set-task-priority">previous post</a> about setting priority in Executer, but I want low priority to all other threads that can be outside this specific Executer scope.</p>
  
  <p>Is answer using <code>ThreadPoolExecutor</code> more relevant to my case?</p>
  
  <blockquote>
  <pre><code>ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, numOfWorkerThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue&lt;Runnable&gt;());
  threadPool.setThreadFactory(new OpJobThreadFactory(Thread.NORM_PRIORITY-2));
  
  public final static class OpJobThreadFactory implements ThreadFactory {
   private int priority;
   public OpJobThreadFactory(int priority) {
    this(priority, true);
  }
  
  @Override
  public Thread newThread(Runnable r) {
    Thread t = new Thread(r, namePrefix + threadNumber.getAndIncrement());
    t.setDaemon(daemon);
    t.setPriority(priority);
   }
  }
  </code></pre>
  </blockquote>
  
  <ul>
  <li>maybe even use <code>Thread.MIN_PRIORITY</code></li>
  </ul>
  
  <p>Or I'm fine with using <code>Executors.newCachedThreadPool()</code></p>
  
  <blockquote>
    <p>Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically <strong>improve the performance of programs that execute many short-lived asynchronous tasks</strong>. </p>
  </blockquote>
  
  <p>Also <strong>should I use Spring bean?</strong> because I need to create pool on demand/request so it seems not needed/wrong </p>
  
  <p><strong>EDIT</strong>
  Should I use <a href="https://www.callicoder.com/spring-boot-actuator/" rel="nofollow noreferrer">Spring Actuator</a> to get this task or other monitoring tool?</p>
  
  <blockquote>
    <p>Spring Boot Actuator module helps you monitor and manage your Spring Boot application by providing production-ready features like health check-up, auditing, metrics gathering, HTTP tracing etc. All of these features can be accessed over JMX or HTTP endpoints.</p>
  </blockquote>