title: Singleton executor Java
body: <p>Hi i am trying to create executor for binrary file downloading, i have around 100-200 files to be downloaded and stored in disk.</p>
  
  <p>This is my DownloadExecutor.java</p>
  
  <pre><code>public final class DownloadExecutor {
  
    private static DownloadExecutor executor;
  
    private ExecutorService executorService;
  
    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
      private final AtomicInteger mCount = new AtomicInteger(1);
  
      public Thread newThread(Runnable r) {
        return new Thread(r, "DownloadExecutor #" + mCount.getAndIncrement());
      }
    };
  
    public static DownloadExecutor getInstance() {
      if (executor == null) {
        synchronized (DownloadExecutor.class) {
          executor = new DownloadExecutor();
        }
      }
      return executor;
    }
  
    private DownloadExecutor() {
      final BlockingQueue&lt;Runnable&gt; sPoolWorkQueue =
          new LinkedBlockingQueue&lt;Runnable&gt;(128);
      final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
      // We want at least 2 threads and at most 4 threads in the core pool,
      // preferring to have 1 less than the CPU count to avoid saturating
      // the CPU with background work
      final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
      final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
      final int KEEP_ALIVE_SECONDS = 2;
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.append("CPU: " + CPU_COUNT);
      stringBuilder.append(",CORE POOL: " + CORE_POOL_SIZE);
      stringBuilder.append(",MAX POOL: " + MAXIMUM_POOL_SIZE);
      System.out.println("Executor log: " + stringBuilder.toString());
      ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
          CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
          sPoolWorkQueue, sThreadFactory);
      threadPoolExecutor.allowCoreThreadTimeOut(true);
      executorService = threadPoolExecutor;
    }
  
    public void execute(Callable&lt;?&gt; callable) {
      System.out.println("Adding");
      executorService.submit(callable);
    }
  }
  </code></pre>
  
  <p>I submit the task using following code
  <code>DownloadExecutor.getInstance().execute(someCallable);</code></p>
  
  <p>Initially when i trigger this all downloads success but when i trigger next time it throws <code>java.util.concurrent.RejectedExecutionException</code>.</p>
  
  <p>Note i do no want to use <code>shutDown()</code> on this, is this wish possible to omit shutDown. I came to know <code>java.util.concurrent.RejectedExecutionException</code> occur when you try to submit a task on terminated service.</p>
  
  <p>Code for calling</p>
  
  <pre><code>for (int i = 0; i &lt; totalVideos; i++) {
        try {
          DownloadExecutor.getInstance().execute(new YoutubeFilewriter(downloadRepository,
              videoDao, listResource.data.get(i), parentPath, YoutubeVideoDownloader.this));
        } catch (IOException e) {
          e.printStackTrace();
          ++failedVideos;
        }
      }
  </code></pre>
  
  <p>Imagine on every button click this code is fired.</p>