Example usage for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

List of usage examples for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

Introduction

In this page you can find the example usage for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor.

Prototype

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
        BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) 

Source Link

Document

Creates a new ThreadPoolExecutor with the given initial parameters.

Usage

From source file:com.mellanox.r4h.DFSClient.java

/**
 * Create hedged reads thread pool, HEDGED_READ_THREAD_POOL, if
 * it does not already exist./*from  w  w w .  j  a v a2s . c  o  m*/
 * 
 * @param num
 *            Number of threads for hedged reads thread pool.
 *            If zero, skip hedged reads thread pool creation.
 */
private synchronized void initThreadsNumForHedgedReads(int num) {
    if (num <= 0 || HEDGED_READ_THREAD_POOL != null)
        return;
    HEDGED_READ_THREAD_POOL = new ThreadPoolExecutor(1, num, 60, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>(), new Daemon.DaemonFactory() {
                private final AtomicInteger threadIndex = new AtomicInteger(0);

                @Override
                public Thread newThread(Runnable r) {
                    Thread t = super.newThread(r);
                    t.setName("hedgedRead-" + threadIndex.getAndIncrement());
                    return t;
                }
            }, new ThreadPoolExecutor.CallerRunsPolicy() {

                @Override
                public void rejectedExecution(Runnable runnable, ThreadPoolExecutor e) {
                    LOG.info("Execution rejected, Executing in current thread");
                    HEDGED_READ_METRIC.incHedgedReadOpsInCurThread();
                    // will run in the current thread
                    super.rejectedExecution(runnable, e);
                }
            });
    HEDGED_READ_THREAD_POOL.allowCoreThreadTimeOut(true);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Using hedged reads; pool threads=" + num);
    }
}