content: /**
   * Copyright (C) 2015 Fernando Cejas 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.sample.data.executor;
  
  import android.support.annotation.NonNull;
  import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor;
  import java.util.concurrent.LinkedBlockingQueue;
  import java.util.concurrent.ThreadFactory;
  import java.util.concurrent.ThreadPoolExecutor;
  import java.util.concurrent.TimeUnit;
  import javax.inject.Inject;
  import javax.inject.Singleton;
  
  /**
   * Decorated {@link java.util.concurrent.ThreadPoolExecutor}
   */
  @Singleton
  public class JobExecutor implements ThreadExecutor {
    private final ThreadPoolExecutor threadPoolExecutor;
  
    @Inject
    JobExecutor() {
      this.threadPoolExecutor = new ThreadPoolExecutor(3, 5, 10, TimeUnit.SECONDS,
          new LinkedBlockingQueue<>(), new JobThreadFactory());
    }
  
    @Override public void execute(@NonNull Runnable runnable) {
      this.threadPoolExecutor.execute(runnable);
    }
  
    private static class JobThreadFactory implements ThreadFactory {
      private int counter = 0;
  
      @Override public Thread newThread(@NonNull Runnable runnable) {
        return new Thread(runnable, "android_" + counter++);
      }
    }
  }
  