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.hive.service.cli.session.SessionManager.java

private void createBackgroundOperationPool() {
    int poolSize = hiveConf.getIntVar(ConfVars.HIVE_SERVER2_ASYNC_EXEC_THREADS);
    LOG.info("HiveServer2: Background operation thread pool size: " + poolSize);
    int poolQueueSize = hiveConf.getIntVar(ConfVars.HIVE_SERVER2_ASYNC_EXEC_WAIT_QUEUE_SIZE);
    LOG.info("HiveServer2: Background operation thread wait queue size: " + poolQueueSize);
    long keepAliveTime = HiveConf.getTimeVar(hiveConf, ConfVars.HIVE_SERVER2_ASYNC_EXEC_KEEPALIVE_TIME,
            TimeUnit.SECONDS);//  w w w .j a va  2  s  .co m
    LOG.info("HiveServer2: Background operation thread keepalive time: " + keepAliveTime + " seconds");

    // Create a thread pool with #poolSize threads
    // Threads terminate when they are idle for more than the keepAliveTime
    // A bounded blocking queue is used to queue incoming operations, if #operations > poolSize
    String threadPoolName = "HiveServer2-Background-Pool";
    backgroundOperationPool = new ThreadPoolExecutor(poolSize, poolSize, keepAliveTime, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(poolQueueSize),
            new ThreadFactoryWithGarbageCleanup(threadPoolName));
    backgroundOperationPool.allowCoreThreadTimeOut(true);

    checkInterval = HiveConf.getTimeVar(hiveConf, ConfVars.HIVE_SERVER2_SESSION_CHECK_INTERVAL,
            TimeUnit.MILLISECONDS);
    sessionTimeout = HiveConf.getTimeVar(hiveConf, ConfVars.HIVE_SERVER2_IDLE_SESSION_TIMEOUT,
            TimeUnit.MILLISECONDS);
    checkOperation = HiveConf.getBoolVar(hiveConf, ConfVars.HIVE_SERVER2_IDLE_SESSION_CHECK_OPERATION);
}

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

private void addExecutorForVolume(final File volume) {
    ThreadFactory threadFactory = new ThreadFactory() {
        int counter = 0;

        @Override//from  w ww  . j a v a 2s  .c  o m
        public Thread newThread(Runnable r) {
            int thisIndex;
            synchronized (this) {
                thisIndex = counter++;
            }
            Thread t = new Thread(threadGroup, r);
            t.setName("Async disk worker #" + thisIndex + " 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.solr.client.solrj.TestBackupLBHttpSolrServer.java

@Override
public void setUp() throws Exception {
    super.setUp();
    httpClient = HttpClientUtil.createClient(null);
    HttpClientUtil.setConnectionTimeout(httpClient, 1000);
    inFlightRequestMonitor = new InflightRequestMonitor(false);
    solr = new HashMap<String, SolrInstance>();
    commExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 5, TimeUnit.SECONDS, // terminate idle threads after 5 sec
            new SynchronousQueue<Runnable>(), // directly hand off tasks
            new DefaultSolrThreadFactory("TestBackupLBHttpSolrServer"));
}

From source file:com.netflix.blitz4j.LoggingConfiguration.java

private LoggingConfiguration() {
    ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(false)
            .setNameFormat("DynamicLog4jListener").build();

    this.executorPool = new ThreadPoolExecutor(0, 1, 15 * 60, TimeUnit.SECONDS, new SynchronousQueue(),
            threadFactory);/*from   w  w w .j av a  2  s.c  om*/
}

From source file:com.github.anba.es6draft.util.TestGlobals.java

protected static ThreadPoolExecutor createDefaultSharedExecutor() {
    int coreSize = 4;
    int maxSize = 12;
    long timeout = 60L;
    int queueCapacity = 10;
    return new ThreadPoolExecutor(coreSize, maxSize, timeout, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(queueCapacity), new ThreadPoolExecutor.CallerRunsPolicy());
}

From source file:net.opentsdb.tsd.GraphHandler.java

/**
 * Constructor.// ww  w. ja  v  a  2 s .  co  m
 */
public GraphHandler() {
    // Gnuplot is mostly CPU bound and does only a little bit of IO at the
    // beginning to read the input data and at the end to write its output.
    // We want to avoid running too many Gnuplot instances concurrently as
    // it can steal a significant number of CPU cycles from us.  Instead, we
    // allow only one per core, and we nice it (the nicing is done in the
    // shell script we use to start Gnuplot).  Similarly, the queue we use
    // is sized so as to have a fixed backlog per core.
    final int ncores = Runtime.getRuntime().availableProcessors();
    gnuplot = new ThreadPoolExecutor(ncores, ncores, // Thread pool of a fixed size.
            /* 5m = */ 300000, MILLISECONDS, // How long to keep idle threads.
            new ArrayBlockingQueue<Runnable>(20 * ncores), // XXX Don't hardcode?
            thread_factory);
    // ArrayBlockingQueue does not scale as much as LinkedBlockingQueue in terms
    // of throughput but we don't need high throughput here.  We use ABQ instead
    // of LBQ because it creates far fewer references.
}

From source file:org.isharding.shard.strategy.access.impl.ParallelShardAccessStrategy.java

private ThreadPoolExecutor buildThreadPoolExecutor() {
    return new ThreadPoolExecutor(10, 50, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), FACTORY);
}

From source file:com.linkedin.drelephant.ElephantRunner.java

@Override
public void run() {
    logger.info("Dr.elephant has started");
    try {/* w  w w . j a va 2 s . c  om*/
        _hadoopSecurity = new HadoopSecurity();
        _hadoopSecurity.doAs(new PrivilegedAction<Void>() {
            @Override
            public Void run() {
                HDFSContext.load();
                loadGeneralConfiguration();
                loadAnalyticJobGenerator();
                ElephantContext.init();

                // Initialize the metrics registries.
                MetricsController.init();

                logger.info("executor num is " + _executorNum);
                if (_executorNum < 1) {
                    throw new RuntimeException("Must have at least 1 worker thread.");
                }
                ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("dr-el-executor-thread-%d")
                        .build();
                _threadPoolExecutor = new ThreadPoolExecutor(_executorNum, _executorNum, 0L,
                        TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), factory);

                while (_running.get() && !Thread.currentThread().isInterrupted()) {
                    _analyticJobGenerator.updateResourceManagerAddresses();
                    lastRun = System.currentTimeMillis();

                    logger.info("Fetching analytic job list...");

                    try {
                        _hadoopSecurity.checkLogin();
                    } catch (IOException e) {
                        logger.info("Error with hadoop kerberos login", e);
                        //Wait for a while before retry
                        waitInterval(_retryInterval);
                        continue;
                    }

                    List<AnalyticJob> todos;
                    try {
                        todos = _analyticJobGenerator.fetchAnalyticJobs();
                    } catch (Exception e) {
                        logger.error("Error fetching job list. Try again later...", e);
                        //Wait for a while before retry
                        waitInterval(_retryInterval);
                        continue;
                    }

                    for (AnalyticJob analyticJob : todos) {
                        _threadPoolExecutor.submit(new ExecutorJob(analyticJob));
                    }

                    int queueSize = _threadPoolExecutor.getQueue().size();
                    MetricsController.setQueueSize(queueSize);
                    logger.info("Job queue size is " + queueSize);

                    //Wait for a while before next fetch
                    waitInterval(_fetchInterval);
                }
                logger.info("Main thread is terminated.");
                return null;
            }
        });
    } catch (Exception e) {
        logger.error(e.getMessage());
        logger.error(ExceptionUtils.getStackTrace(e));
    }
}

From source file:org.perfcake.examples.weaver.Weaver.java

/**
 * Initializes the configuration./*  w  ww.j  a v a  2s .c om*/
 *
 * @throws IOException
 *       When it was not possible to parse the configuration.
 */
public void init() throws IOException {
    int maxThreads = Files.lines(Paths.get(config)).collect(Collectors.summingInt(this::parseWorker));
    if (threads > maxThreads || threads == 0) {
        if (threads > maxThreads) {
            log.warn("Maximum possible threads is " + maxThreads + ", while you requested " + threads
                    + ". Using " + maxThreads + ".");
        }
        threads = maxThreads;
    }

    if (shuffle) {
        log.info("Shuffling workers...");

        final List<Worker> workersList = workers.stream().collect(Collectors.toList());
        Collections.shuffle(workersList);
        workers.clear();
        workersList.forEach(workers::offer);
    }

    log.info("Creating executor with " + threads + " threads.");

    executor = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(),
            new ThreadFactoryBuilder().setDaemon(true).setNameFormat("worker-thread-%d").build());
}

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

public ExecutorService backgroundTasksExecutor0() {
    final ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("background-%03d").setDaemon(true)
            .build();//from   w w  w.  java  2 s  .  co m

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

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