Example usage for java.util.concurrent ThreadPoolExecutor setKeepAliveTime

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

Introduction

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

Prototype

public void setKeepAliveTime(long time, TimeUnit unit) 

Source Link

Document

Sets the thread keep-alive time, which is the amount of time that threads may remain idle before being terminated.

Usage

From source file:org.apache.hadoop.hbase.ipc.TestFifoRpcScheduler.java

private ThreadPoolExecutor disableHandlers(RpcScheduler scheduler) {
    ThreadPoolExecutor rpcExecutor = null;

    try {//from   www  . j a  va2s .  co  m
        Field ExecutorField = scheduler.getClass().getDeclaredField("executor");
        ExecutorField.setAccessible(true);

        scheduler.start();
        rpcExecutor = (ThreadPoolExecutor) ExecutorField.get(scheduler);

        rpcExecutor.setMaximumPoolSize(1);
        rpcExecutor.allowCoreThreadTimeOut(true);
        rpcExecutor.setCorePoolSize(0);
        rpcExecutor.setKeepAliveTime(1, TimeUnit.MICROSECONDS);

        // Wait for 2 seconds, so that idle threads will die
        Thread.sleep(2000);

    } catch (NoSuchFieldException e) {
        LOG.error("No such field exception:" + e);
    } catch (IllegalAccessException e) {
        LOG.error("Illegal access exception:" + e);
    } catch (InterruptedException e) {
        LOG.error("Interrupted exception:" + e);
    }

    return rpcExecutor;
}

From source file:org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexProviderService.java

private ExecutorService createExecutor() {
    ThreadPoolExecutor executor = new ThreadPoolExecutor(0, 5, 60L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
                private final AtomicInteger counter = new AtomicInteger();
                private final Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
                    @Override//from  w w  w .j a va2  s.  co m
                    public void uncaughtException(Thread t, Throwable e) {
                        log.warn("Error occurred in asynchronous processing ", e);
                    }
                };

                @Override
                public Thread newThread(@Nonnull Runnable r) {
                    Thread thread = new Thread(r, createName());
                    thread.setDaemon(true);
                    thread.setPriority(Thread.MIN_PRIORITY);
                    thread.setUncaughtExceptionHandler(handler);
                    return thread;
                }

                private String createName() {
                    return "oak-lucene-" + counter.getAndIncrement();
                }
            });
    executor.setKeepAliveTime(1, TimeUnit.MINUTES);
    executor.allowCoreThreadTimeOut(true);
    return executor;
}