content: /**
   * Copyright (C) 2015 android10.org Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package com.fernandocejas.android10.rx.sample.executor;
  
  import java.util.concurrent.BlockingQueue;
  import java.util.concurrent.Executor;
  import java.util.concurrent.LinkedBlockingQueue;
  import java.util.concurrent.ThreadPoolExecutor;
  import java.util.concurrent.TimeUnit;
  
  public class JobExecutor implements Executor {
  
    private static class LazyHolder {
      private static final JobExecutor INSTANCE = new JobExecutor();
    }
  
    public static JobExecutor getInstance() {
      return LazyHolder.INSTANCE;
    }
  
    private static final int INITIAL_POOL_SIZE = 3;
    private static final int MAX_POOL_SIZE = 5;
  
    // Sets the amount of time an idle thread waits before terminating
    private static final int KEEP_ALIVE_TIME = 10;
  
    // Sets the Time Unit to seconds
    private static final TimeUnit KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;
  
    private final BlockingQueue<Runnable> workQueue;
  
    private final ThreadPoolExecutor threadPoolExecutor;
  
    private JobExecutor() {
      this.workQueue = new LinkedBlockingQueue<>();
      this.threadPoolExecutor = new ThreadPoolExecutor(INITIAL_POOL_SIZE, MAX_POOL_SIZE,
          KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, workQueue);
    }
  
    @Override public void execute(Runnable runnable) {
      if (runnable == null) {
        throw new IllegalArgumentException("Runnable to execute cannot be null");
      }
      threadPoolExecutor.execute(runnable);
    }
  }
  