create Thread Pool - Java java.util.concurrent

Java examples for java.util.concurrent:ThreadPoolExecutor

Description

create Thread Pool

Demo Code


//package com.java2s;

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

public class Main {
    private static final int AVAILABLE_PROCESSORS = Runtime.getRuntime()
            .availableProcessors();//from ww  w  .  j a v  a 2  s.  c  o  m
    private static final int DEFAULT_WORK_QUEUE_CAPACITY = 5000;

    public static ThreadPoolExecutor createThreadPool(
            int processorMultiplier) {
        return creteThreadPool(processorMultiplier,
                DEFAULT_WORK_QUEUE_CAPACITY);
    }

    public static ThreadPoolExecutor creteThreadPool(
            int processorMultiplier, int workQueueCapacity) {
        final int corePoolSize = AVAILABLE_PROCESSORS * processorMultiplier;
        final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                corePoolSize, corePoolSize * 2, 1, TimeUnit.DAYS,
                new LinkedBlockingQueue<Runnable>(workQueueCapacity));
        threadPoolExecutor
                .setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return threadPoolExecutor;
    }
}

Related Tutorials