Java Thread Executor Pool getTightThreadPool()

Here you can find the source of getTightThreadPool()

Description

get Tight Thread Pool

License

Open Source License

Return

an with One worker thread for each CPUm A and a

Declaration

public static ExecutorService getTightThreadPool() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.ExecutorService;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
    private static final int NUM_CPU = Runtime.getRuntime().availableProcessors();

    /**/*w w  w .j a va  2s  .  c o m*/
     * @return an {@link ExecutorService} with One worker thread for each CPUm A {@link SynchronousQueue} and a {@link ThreadPoolExecutor.CallerRunsPolicy}
     */
    public static ExecutorService getTightThreadPool() {
        return getTightThreadPool(1d);
    }

    /**
     * @param threadToCpuRatio - for example, assuming you have 2 CPUs and setting a threadToCpuRation to 3, the result will be a pool with 6 working threads.  
     * @return an {@link ExecutorService} with defined amount of worker thread for each CPUm A {@link SynchronousQueue} and a {@link ThreadPoolExecutor.CallerRunsPolicy}
     */
    public static ExecutorService getTightThreadPool(double threadToCpuRatio) {
        int workingThreads = Double.valueOf(NUM_CPU * threadToCpuRatio).intValue();
        return new ThreadPoolExecutor(workingThreads, workingThreads, 60, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>(true), new ThreadPoolExecutor.CallerRunsPolicy());
    }
}

Related

  1. getPool()
  2. getQueuedThreadPool(double threadToCpuRatio, int queueCapacity)
  3. getRecheckThreadPool()
  4. getThreadPool()
  5. getThrealPool()
  6. normalShutdown(ExecutorService pool, int timeout, TimeUnit timeUnit)
  7. safeClose(ExecutorService pool)