Example usage for java.util.concurrent ThreadPoolExecutor allowCoreThreadTimeOut

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

Introduction

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

Prototype

boolean allowCoreThreadTimeOut

To view the source code for java.util.concurrent ThreadPoolExecutor allowCoreThreadTimeOut.

Click Source Link

Document

If false (default), core threads stay alive even when idle.

Usage

From source file:Main.java

@SuppressLint("NewApi")
public static void allowCoreThreadTimeout(ThreadPoolExecutor executor, boolean value) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        executor.allowCoreThreadTimeOut(value);
    }/*from   w  w  w .j a va2  s  .  c o  m*/
}

From source file:Main.java

public static ExecutorService newDynamicSingleThreadedExecutor() {
    ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());
    executor.allowCoreThreadTimeOut(true);

    return executor;
}

From source file:Main.java

public static ThreadPoolExecutor newThreadPool(int maximumNumOfThreads, String poolName) {
    BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(CAPACITY);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(maximumNumOfThreads, maximumNumOfThreads, 1,
            TimeUnit.SECONDS, workQueue, createFactory(poolName));
    threadPoolExecutor.allowCoreThreadTimeOut(true);
    return threadPoolExecutor;
}

From source file:android.concurrent.ThreadPool.java

private static ThreadPoolExecutor createThreadPoolExecutor(String poolName) {
    ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(DEFAULT_BLOCKING_QUEUE_SIZE);
    //      CallerRunsPolicy policy = new CallerRunsPolicy();
    ThreadPoolExecutor.DiscardPolicy policy = new ThreadPoolExecutor.DiscardPolicy();
    ThreadPoolExecutor excuter = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME,
            UNIT, queue, new PoolThreadFactory(poolName), policy);
    excuter.prestartAllCoreThreads();/*from   w  ww  .  j  a va  2s  .c o m*/
    excuter.allowCoreThreadTimeOut(true);

    return excuter;
}

From source file:org.apache.hadoop.hbase.util.Threads.java

/**
 * Create a new CachedThreadPool with a bounded number as the maximum 
 * thread size in the pool./*from ww w. ja  va2  s. c  om*/
 * 
 * @param maxCachedThread the maximum thread could be created in the pool
 * @param timeout the maximum time to wait
 * @param unit the time unit of the timeout argument
 * @param threadFactory the factory to use when creating new threads
 * @return threadPoolExecutor the cachedThreadPool with a bounded number 
 * as the maximum thread size in the pool. 
 */
public static ThreadPoolExecutor getBoundedCachedThreadPool(int maxCachedThread, long timeout, TimeUnit unit,
        ThreadFactory threadFactory) {
    ThreadPoolExecutor boundedCachedThreadPool = new ThreadPoolExecutor(maxCachedThread, maxCachedThread,
            timeout, unit, new LinkedBlockingQueue<Runnable>(), threadFactory);
    // allow the core pool threads timeout and terminate
    boundedCachedThreadPool.allowCoreThreadTimeOut(true);
    return boundedCachedThreadPool;
}

From source file:com.ery.estorm.util.Threads.java

/**
 * Create a new CachedThreadPool with a bounded number as the maximum thread size in the pool.
 * /*from   ww w . jav  a 2s .c o  m*/
 * @param maxCachedThread
 *            the maximum thread could be created in the pool
 * @param timeout
 *            the maximum time to wait
 * @param unit
 *            the time unit of the timeout argument
 * @param threadFactory
 *            the factory to use when creating new threads
 * @return threadPoolExecutor the cachedThreadPool with a bounded number as the maximum thread size in the pool.
 */
public static ThreadPoolExecutor getBoundedCachedThreadPool(int maxCachedThread, long timeout, TimeUnit unit,
        ThreadFactory threadFactory) {
    ThreadPoolExecutor boundedCachedThreadPool = new ThreadPoolExecutor(maxCachedThread, maxCachedThread,
            timeout, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);
    // allow the core pool threads timeout and terminate
    boundedCachedThreadPool.allowCoreThreadTimeOut(true);
    return boundedCachedThreadPool;
}

From source file:com.amazonaws.services.kinesis.leases.impl.LeaseCoordinator.java

/**
 * Returns executor service that should be used for lease renewal.
 * @param maximumPoolSize Maximum allowed thread pool size
 * @return Executor service that should be used for lease renewal.
 *//*from  w  ww . jav a  2s .c  om*/
private static ExecutorService getLeaseRenewalExecutorService(int maximumPoolSize) {
    ThreadPoolExecutor exec = new ThreadPoolExecutor(maximumPoolSize, maximumPoolSize, 60L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(), LEASE_RENEWAL_THREAD_FACTORY);
    exec.allowCoreThreadTimeOut(true);
    return exec;
}

From source file:com.turn.ttorrent.common.TorrentCreator.java

/**
 * Creates a new executor suitable for torrent hashing.
 * //from w  w w.ja  v a  2s.  co  m
 * This executor controls memory usage by using a bounded queue, and the
 * CallerRunsPolicy slows down the producer if the queue bound is exceeded.
 * The requirement is then to make the queue large enough to keep all the
 * executor threads busy if the producer executes a task itself.
 * 
 * In terms of memory, Executor.execute is much more efficient than
 * ExecutorService.submit, and ByteBuffer(s) released by the ChunkHasher(s)
 * remain in eden space, so are rapidly recycled for reading the next
 * block(s). JVM ergonomics can make this much more efficient than any
 * heap-based strategy we might devise. Now, we want the queue size small
 * enough that JVM ergonomics keeps the eden size small.
 */
@Nonnull
public static ThreadPoolExecutor newExecutor(@Nonnull String peerName) {
    int threads = getHashingThreadsCount();
    logger.info("Creating ExecutorService with {} threads", new Object[] { threads });
    ThreadFactory factory = new DefaultThreadFactory("bittorrent-executor-" + peerName, true);
    ThreadPoolExecutor service = new ThreadPoolExecutor(0, threads, 1L, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(threads * 3), factory, new ThreadPoolExecutor.CallerRunsPolicy());
    service.allowCoreThreadTimeOut(true);
    return service;
}

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

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

    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:io.bitsquare.common.util.Utilities.java

public static ThreadPoolExecutor getThreadPoolExecutor(String name, int corePoolSize, int maximumPoolSize,
        long keepAliveTimeInSec) {
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(name).setDaemon(true).build();
    ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTimeInSec,
            TimeUnit.SECONDS, new ArrayBlockingQueue<>(maximumPoolSize), threadFactory);
    executor.allowCoreThreadTimeOut(true);
    executor.setRejectedExecutionHandler((r, e) -> {
        log.debug("RejectedExecutionHandler called");
    });//from www . java 2s. com
    return executor;
}