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.tez.dag.app.taskclean.TaskCleanerImpl.java

public void serviceStart() {
    ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("TaskCleaner #%d").build();
    launcherPool = new ThreadPoolExecutor(5, 5, 1, TimeUnit.HOURS, new LinkedBlockingQueue<Runnable>(), tf);
    eventHandlingThread = new Thread(new Runnable() {
        @Override/*from   w  w w  .j  a v  a  2s . co m*/
        public void run() {
            TaskCleanupEvent event = null;
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    event = eventQueue.take();
                } catch (InterruptedException e) {
                    LOG.error("Returning, interrupted : " + e);
                    return;
                }
                // the events from the queue are handled in parallel
                // using a thread pool
                launcherPool.execute(new EventProcessor(event));
            }
        }
    });
    eventHandlingThread.setName("TaskCleaner Event Handler");
    eventHandlingThread.start();
}

From source file:com.buaa.cfs.nfs3.AsyncDataService.java

public AsyncDataService() {
    threadFactory = new ThreadFactory() {
        @Override/*from www  .j av a 2 s  .  co  m*/
        public Thread newThread(Runnable r) {
            return new Thread(threadGroup, r);
        }
    };

    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);
}

From source file:org.apache.hadoop.mapreduce.v2.app.taskclean.TaskCleanerImpl.java

public void start() {
    ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("TaskCleaner #%d").build();
    launcherPool = new ThreadPoolExecutor(5, 5, 1, TimeUnit.HOURS, new LinkedBlockingQueue<Runnable>(), tf);
    eventHandlingThread = new Thread(new Runnable() {
        @Override//from www. j av a  2  s. c o  m
        public void run() {
            TaskCleanupEvent event = null;
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    event = eventQueue.take();
                } catch (InterruptedException e) {
                    LOG.error("Returning, interrupted : " + e);
                    return;
                }
                // the events from the queue are handled in parallel
                // using a thread pool
                launcherPool.execute(new EventProcessor(event));
            }
        }
    });
    eventHandlingThread.setName("TaskCleaner Event Handler");
    eventHandlingThread.start();
    super.start();
}

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   w  w  w  .ja va 2 s. c  o m*/
    return executor;
}

From source file:org.nebula.framework.core.EventPoller.java

public EventPoller(NebulaClient nebulaClient, E nodeDefinition, List<String> realms,
        Configuration configuration) {
    this.nebulaClient = nebulaClient;
    this.nodeDefinition = nodeDefinition;
    this.realms = realms;
    this.configuration = configuration;

    int maxExecutionThreads = configuration.getMaxExecutionThreads();

    this.nodeExecutor = new ThreadPoolExecutor(1, maxExecutionThreads, 60L, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(maxExecutionThreads, true),
            new ThreadPoolExecutor.CallerRunsPolicy());
}

From source file:com.sm.store.server.StoreServerHandler.java

private void init() {
    if (Runtime.getRuntime().availableProcessors() > maxThreads)
        this.maxThreads = Runtime.getRuntime().availableProcessors();
    if (maxQueue < maxThreads * 1000)
        maxQueue = maxThreads * 1000;// ww  w  . ja  v a 2 s .  c o  m
    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(maxQueue);
    threadPools = new ThreadPoolExecutor(maxThreads, maxThreads, 30, TimeUnit.SECONDS, queue,
            new ThreadPoolFactory("store"));
}

From source file:org.apache.hadoop.hbase.master.MasterMobCompactionThread.java

public MasterMobCompactionThread(HMaster master) {
    this.master = master;
    this.conf = master.getConfiguration();
    final String n = Thread.currentThread().getName();
    // this pool is used to run the mob compaction
    this.masterMobPool = new ThreadPoolExecutor(1, 2, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),
            new ThreadFactory() {
                @Override/*www  .  j  a v  a2  s .  com*/
                public Thread newThread(Runnable r) {
                    Thread t = new Thread(r);
                    t.setName(n + "-MasterMobCompaction-" + EnvironmentEdgeManager.currentTime());
                    return t;
                }
            });
    ((ThreadPoolExecutor) this.masterMobPool).allowCoreThreadTimeOut(true);
    // this pool is used in the mob compaction to compact the mob files by partitions
    // in parallel
    this.mobCompactorPool = MobUtils.createMobCompactorThreadPool(master.getConfiguration());
}

From source file:org.apache.hadoop.hbase.master.MasterMobFileCompactionThread.java

public MasterMobFileCompactionThread(HMaster master) {
    this.master = master;
    this.conf = master.getConfiguration();
    final String n = Thread.currentThread().getName();
    // this pool is used to run the mob file compaction
    this.masterMobPool = new ThreadPoolExecutor(1, 2, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),
            new ThreadFactory() {
                @Override//from  w w w . j  av a 2s  .  c  o  m
                public Thread newThread(Runnable r) {
                    Thread t = new Thread(r);
                    t.setName(n + "-MasterMobFileCompaction-" + EnvironmentEdgeManager.currentTime());
                    return t;
                }
            });
    ((ThreadPoolExecutor) this.masterMobPool).allowCoreThreadTimeOut(true);
    // this pool is used in the mob file compaction to compact the mob files by partitions
    // in parallel
    this.mobFileCompactorPool = MobUtils.createMobFileCompactorThreadPool(master.getConfiguration());
}

From source file:org.openstreetmap.josm.data.imagery.TMSCachedTileLoader.java

/**
 * @param nameFormat see {@link Utils#newThreadFactory(String, int)}
 * @param workers number of worker thread to keep
 * @return new ThreadPoolExecutor that will use a @see HostLimitQueue based queue
 */// www .  ja v  a2  s . co  m
public static ThreadPoolExecutor getNewThreadPoolExecutor(String nameFormat, int workers) {
    HostLimitQueue workQueue = new HostLimitQueue(HOST_LIMIT.get().intValue());
    ThreadPoolExecutor executor = new ThreadPoolExecutor(0, // 0 so for unused thread pools threads will eventually die, freeing also the threadpool
            workers, // do not this number of threads
            300, // keepalive for thread
            TimeUnit.SECONDS, workQueue, Utils.newThreadFactory(nameFormat, Thread.NORM_PRIORITY));
    workQueue.setExecutor(executor);
    return executor;
}

From source file:com.addthis.codec.utils.ExecutorServiceBuilder.java

public ExecutorService build() {
    ThreadPoolExecutor service = new ThreadPoolExecutor(coreThreads, maxThreads, keepAlive,
            TimeUnit.MILLISECONDS, queue, threadFactory);
    if (shutdownHook) {
        return MoreExecutors.getExitingExecutorService(service);
    } else {/*from w  ww . java 2  s  . c o m*/
        return service;
    }
}