Commits

Penny Wyatt committed 89072ad

Finally got threading working as intended. Now uses up to 50 threads instance-wide for concurrently calling Stash, and shuts them down when idle.

  • Participants
  • Parent commits 10ef2ec

Comments (0)

Files changed (1)

File src/main/java/com/atlassianqa/jiraqahelper/services/hints/HintServiceImpl.java

 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.beans.factory.InitializingBean;
 
 
 public class HintServiceImpl implements HintService, InitializingBean, DisposableBean
 {
-    private ExecutorService executor = null;
+    private ThreadPoolExecutor executor = null;
     private ThreadLocalDelegateExecutorFactory threadFactory;
-    private final int MIN_CONCURRENT_THREADS = 10;
     private final int MAX_CONCURRENT_THREADS = 50;
     private final int STASH_TIMEOUT_MILLIS = 10000;
     private final int THREAD_TIMEOUT_MILLIS = 5000;
     
     public void afterPropertiesSet() throws Exception 
     {
-        executor = new ThreadPoolExecutor (MIN_CONCURRENT_THREADS, MAX_CONCURRENT_THREADS, THREAD_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
+        // The core thread pool size is set to the maximum value because the thread pool doesn't work as you'd think.
+        // If min were set to 10, and max to 50, and we keep the items in a LinkedBlockingQueue, it will never go above 10 threads.
+        // This is because it only creates a new thread when the LinkedBlockingQueue rejects a new callable, which it never will.
+        // Instead, we'll set the minimum to the maximum and get the executor to shut down idle threads. This gives us the intended effect.
+        executor = new ThreadPoolExecutor (MAX_CONCURRENT_THREADS, MAX_CONCURRENT_THREADS, THREAD_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
+        executor.allowCoreThreadTimeOut (true);
     }
     
     public void destroy() throws Exception 
         @Override
         public Pair<FileDiff,List<Rule>> call() throws StashException 
         {
-            Logger log = LoggerFactory.getLogger(HintServiceImpl.class);
-            log.error(Thread.currentThread().getName());
             List<Rule> matchedRules = new ArrayList<Rule> ();
             for (Rule rule : rules)
             {