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, RejectedExecutionHandler handler) 

Source Link

Document

Creates a new ThreadPoolExecutor with the given initial parameters and Executors#defaultThreadFactory default thread factory .

Usage

From source file:org.apache.hadoop.hdfs.server.datanode.FSDatasetAsyncDiskService.java

/**
 * Create a AsyncDiskServices with a set of volumes (specified by their
 * root directories).//from   w  w  w  .  ja  va 2  s  .c  o  m
 * 
 * The AsyncDiskServices uses one ThreadPool per volume to do the async
 * disk operations.
 * 
 * @param volumes The roots of the data volumes.
 */
FSDatasetAsyncDiskService(File[] volumes) {

    threadFactory = new ThreadFactory() {
        public Thread newThread(Runnable r) {
            return new Thread(threadGroup, r);
        }
    };

    // Create one ThreadPool per volume
    for (int v = 0; v < volumes.length; v++) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_THREADS_PER_VOLUME,
                MAXIMUM_THREADS_PER_VOLUME, THREADS_KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(), threadFactory);

        // This can reduce the number of running threads
        executor.allowCoreThreadTimeOut(true);
        executors.put(volumes[v], executor);
    }

}

From source file:AIR.Common.Threading.BoundedThreadPool.java

public BoundedThreadPool(int threadCount, String threadPoolName, int highWaterMark, int lowWaterMark,
        IThreadPoolStatsRecorder statsRecorder) {
    if (threadCount <= 0) {
        threadCount = Math.max(_numberOfProcessors, 2);
    }//ww  w.j a v a 2s  .  c o m
    _name = StringUtils.defaultString(threadPoolName);
    _taskQHighWaterMark = highWaterMark <= 0 ? Integer.MAX_VALUE : highWaterMark;
    _taskQLowWaterMark = lowWaterMark <= 0 ? highWaterMark : lowWaterMark;
    if (lowWaterMark > highWaterMark) {
        throw new IllegalArgumentException("The low watermark cannot be larger than the high watermark");
    }

    if (statsRecorder != null) {
        statsRecorder.setThreadPool(this);
    }
    _statsRecorder = statsRecorder;
    _taskQueue = new ArrayBlockingQueue<>(_taskQHighWaterMark, true);
    ThreadFactory threadFactory = new NamedThreadFactory();
    _workerThreadPool = new ThreadPoolExecutor(threadCount, threadCount, 0, TimeUnit.NANOSECONDS, _taskQueue,
            threadFactory);
    synchronized (_statusLock) {
        _workerThreadPool.prestartAllCoreThreads();
        _status = ThreadPoolStatus.Active;
    }
}

From source file:natalia.dymnikova.util.ThreadPoolBeans.java

public ExecutorService firstPriorityTasksExecutor0() {
    final ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("first-priority-%03d")
            .setDaemon(true).build();/*  www.j a v a  2 s  .c om*/

    final ExecutorService executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
            new SynchronousQueue<>(), factory);

    log.debug("Constructed first priority tasks executor {}", executor);
    return executor;
}

From source file:org.openscore.worker.management.services.WorkerManager.java

@PostConstruct
private void init() {
    logger.info("Initialize worker with UUID: " + workerUuid);
    System.setProperty("worker.uuid", workerUuid); //do not remove!!!

    executorService = new ThreadPoolExecutor(numberOfThreads, numberOfThreads, Long.MAX_VALUE,
            TimeUnit.NANOSECONDS, inBuffer, new WorkerThreadFactory("WorkerExecutionThread"));

    mapOfRunningTasks = new ConcurrentHashMap<>(numberOfThreads);
}

From source file:org.apache.kylin.storage.hbase.HBaseConnection.java

public static ExecutorService getCoprocessorPool() {
    if (coprocessorPool != null) {
        return coprocessorPool;
    }/*from   ww w  . ja  v  a  2  s  . c o m*/

    synchronized (HBaseConnection.class) {
        if (coprocessorPool != null) {
            return coprocessorPool;
        }

        KylinConfig config = KylinConfig.getInstanceFromEnv();

        // copy from HConnectionImplementation.getBatchPool()
        int maxThreads = config.getHBaseMaxConnectionThreads();
        int coreThreads = config.getHBaseCoreConnectionThreads();
        long keepAliveTime = config.getHBaseConnectionThreadPoolAliveSeconds();
        LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(maxThreads * 100);
        ThreadPoolExecutor tpe = new ThreadPoolExecutor(coreThreads, maxThreads, keepAliveTime,
                TimeUnit.SECONDS, workQueue, //
                Threads.newDaemonThreadFactory("kylin-coproc-"));
        tpe.allowCoreThreadTimeOut(true);

        logger.info("Creating coprocessor thread pool with max of {}, core of {}", maxThreads, coreThreads);

        coprocessorPool = tpe;
        return coprocessorPool;
    }
}

From source file:org.apache.lens.server.api.events.AsyncEventListener.java

/**
 * Create an asynchronous event listener which uses a thread poool to process events.
 *
 * @param poolSize       size of the event processing pool
 * @param timeOutSeconds time out in seconds when an idle thread is destroyed
 * @param isDaemon       if the threads used to process should be daemon threads,
 *                       if false, then implementation should call stop()
 *                       to stop the thread pool
 *//* w ww  . j  ava2  s. c o m*/
public AsyncEventListener(int poolSize, long timeOutSeconds, final boolean isDaemon) {
    eventQueue = new LinkedBlockingQueue<>();

    ThreadFactory factory = new BasicThreadFactory.Builder().namingPattern(getName() + "_AsyncThread-%d")
            .daemon(isDaemon).priority(Thread.NORM_PRIORITY).build();
    // fixed pool with min and max equal to poolSize
    processor = new ThreadPoolExecutor(poolSize, poolSize, timeOutSeconds, TimeUnit.SECONDS, eventQueue,
            factory);
    processor.allowCoreThreadTimeOut(true);
}

From source file:org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.RamDiskAsyncLazyPersistService.java

private void addExecutorForVolume(final File volume) {
    ThreadFactory threadFactory = new ThreadFactory() {

        @Override/*w  w w . ja  v  a2s.com*/
        public Thread newThread(Runnable r) {
            Thread t = new Thread(threadGroup, r);
            t.setName("Async RamDisk lazy persist worker for volume " + volume);
            return t;
        }
    };

    ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_THREADS_PER_VOLUME, MAXIMUM_THREADS_PER_VOLUME,
            THREADS_KEEP_ALIVE_SECONDS, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);

    // This can reduce the number of running threads
    executor.allowCoreThreadTimeOut(true);
    executors.put(volume, executor);
}

From source file:org.apache.synapse.commons.executors.PriorityExecutor.java

/**
 * Initialize the executor by using the properties. Create the queues
 * and ThreadPool executor./* ww  w .j  av  a  2s. com*/
 */
public void init() {
    if (queue == null) {
        throw new IllegalStateException("Queue should be specified before initializing");
    }

    executor = new ThreadPoolExecutor(core, max, keepAlive, TimeUnit.SECONDS, queue, new NativeThreadFactory(
            new ThreadGroup("executor-group"), "priority-worker" + (name != null ? "-" + name : "")));

    initialzed = true;

    if (log.isDebugEnabled()) {
        log.debug("Started the thread pool executor with threads, " + "core = " + core + " max = " + max
                + ", keep-alive = " + keepAlive);
    }
}

From source file:org.nuxeo.ecm.core.event.impl.PostCommitEventExecutor.java

public PostCommitEventExecutor() {
    // use as much thread as needed up to MAX_POOL_SIZE
    // keep them alive a moment for reuse
    // have all threads torn down when there is no work to do
    ThreadFactory threadFactory = new NamedThreadFactory("Nuxeo-Event-PostCommit-");
    executor = new ThreadPoolExecutor(0, MAX_POOL_SIZE, KEEP_ALIVE_TIME_SECOND, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>(), threadFactory);
    ((ThreadPoolExecutor) executor).allowCoreThreadTimeOut(true);
}

From source file:org.apache.hadoop.hbase.procedure.ProcedureCoordinator.java

/**
 * Default thread pool for the procedure
 *
 * @param coordName// w  ww. j  a v a 2s.c  o m
 * @param opThreads the maximum number of threads to allow in the pool
 * @param keepAliveMillis the maximum time (ms) that excess idle threads will wait for new tasks
 */
public static ThreadPoolExecutor defaultPool(String coordName, int opThreads, long keepAliveMillis) {
    return new ThreadPoolExecutor(1, opThreads, keepAliveMillis, TimeUnit.MILLISECONDS,
            new SynchronousQueue<Runnable>(),
            new DaemonThreadFactory("(" + coordName + ")-proc-coordinator-pool"));
}