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) 

Source Link

Document

Creates a new ThreadPoolExecutor with the given initial parameters, the default thread factory and the default rejected execution handler.

Usage

From source file:com.pinterest.rocksplicator.controller.DispatcherTest.java

@Test
public void testNoPendingTask() throws Exception {
    // Assuming there is no task in the queue in the test.
    PowerMockito.when(taskQueue.dequeueTask(anyString())).thenReturn(null);
    Semaphore idleWorkersSemaphore = new Semaphore(1);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1));
    WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, taskQueue);
    TaskDispatcher dispatcher = new TaskDispatcher(1, idleWorkersSemaphore, workerPool, taskQueue);
    dispatcher.start();/*from  w  w  w  . ja  v  a 2  s .c o  m*/
    Thread.sleep(1000);
    Assert.assertEquals(1, idleWorkersSemaphore.availablePermits());
    Thread.sleep(1000);
    Assert.assertEquals(1, idleWorkersSemaphore.availablePermits());
    dispatcher.stop();
}

From source file:com.l2jfree.gameserver.ThreadPoolManager.java

private ThreadPoolManager() {
    final int instantPoolSize = Math.max(1, Config.THREAD_POOL_SIZE / 3);

    _scheduledPool = new ScheduledThreadPoolExecutor(Config.THREAD_POOL_SIZE - instantPoolSize);
    _scheduledPool.setRejectedExecutionHandler(new L2RejectedExecutionHandler());
    _scheduledPool.prestartAllCoreThreads();

    _instantPool = new ThreadPoolExecutor(instantPoolSize, instantPoolSize, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(100000));
    _instantPool.setRejectedExecutionHandler(new L2RejectedExecutionHandler());
    _instantPool.prestartAllCoreThreads();

    _longRunningPool = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>());
    _longRunningPool.setRejectedExecutionHandler(new L2RejectedExecutionHandler());
    _longRunningPool.prestartAllCoreThreads();

    scheduleAtFixedRate(new Runnable() {
        @Override//from   w w  w  .  ja v  a 2  s .co m
        public void run() {
            purge();
        }
    }, 60000, 60000);

    _log.info("ThreadPoolManager: Initialized with " + _scheduledPool.getPoolSize() + " scheduler, "
            + _instantPool.getPoolSize() + " instant, " + _longRunningPool.getPoolSize()
            + " long running thread(s).");
}

From source file:com.l2jfree.util.concurrent.L2ThreadPool.java

public static void initThreadPools(ThreadPoolInitializer initializer) throws Exception {
    if (!ArrayUtils.isEmpty(_scheduledPools) || !ArrayUtils.isEmpty(_instantPools)
            || !ArrayUtils.isEmpty(_longRunningPools))
        throw new Exception("The thread pool has been already set!");

    initializer.initThreadPool();/*from  www .j ava 2 s .c  om*/

    _scheduledPools = initializer.getScheduledPools();
    _instantPools = initializer.getInstantPools();
    _longRunningPools = initializer.getLongRunningPools();

    if (ArrayUtils.isEmpty(_scheduledPools)) {
        _log.info("No scheduled thread pool has been manually initialized, so initializing default one.");

        _scheduledPools = new ScheduledThreadPoolExecutor[] { new ScheduledThreadPoolExecutor( //
                // int corePoolSize
                4) };
    }

    if (ArrayUtils.isEmpty(_instantPools)) {
        _log.info("No instant thread pool has been manually initialized, so initializing default one.");

        _instantPools = new ThreadPoolExecutor[] { new ThreadPoolExecutor( //
                // int corePoolSize
                0,
                // int maximumPoolSize
                Integer.MAX_VALUE,
                // long keepAliveTime
                60L,
                // TimeUnit unit
                TimeUnit.SECONDS,
                // BlockingQueue<Runnable> workQueue
                new SynchronousQueue<Runnable>()) };
    }

    if (ArrayUtils.isEmpty(_longRunningPools)) {
        _log.info("No long running thread pool has been manually initialized, so initializing default one.");

        _longRunningPools = new ThreadPoolExecutor[] { new ThreadPoolExecutor( //
                // int corePoolSize
                0,
                // int maximumPoolSize
                Integer.MAX_VALUE,
                // long keepAliveTime
                60L,
                // TimeUnit unit
                TimeUnit.SECONDS,
                // BlockingQueue<Runnable> workQueue
                new SynchronousQueue<Runnable>()) };
    }

    for (ThreadPoolExecutor threadPool : getThreadPools()) {
        threadPool.setRejectedExecutionHandler(new L2RejectedExecutionHandler());
        threadPool.prestartAllCoreThreads();
    }

    scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            purge();
        }
    }, 60000, 60000);

    _log.info("L2ThreadPool: Initialized with");
    _log.info("\t... " + getPoolSize(_scheduledPools) + "/" + getMaximumPoolSize(_scheduledPools)
            + " scheduler,");
    _log.info("\t... " + getPoolSize(_instantPools) + "/" + getMaximumPoolSize(_instantPools) + " instant,");
    _log.info("\t... " + getPoolSize(_longRunningPools) + "/" + getMaximumPoolSize(_longRunningPools)
            + " long running thread(s).");
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.amlauncher.ApplicationMasterLauncher.java

@Override
protected void serviceInit(Configuration conf) throws Exception {
    int threadCount = conf.getInt(YarnConfiguration.RM_AMLAUNCHER_THREAD_COUNT,
            YarnConfiguration.DEFAULT_RM_AMLAUNCHER_THREAD_COUNT);
    ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("ApplicationMasterLauncher #%d").build();
    launcherPool = new ThreadPoolExecutor(threadCount, threadCount, 1, TimeUnit.HOURS,
            new LinkedBlockingQueue<Runnable>());
    launcherPool.setThreadFactory(tf);//from w  w  w . ja v a  2  s.  com

    Configuration newConf = new YarnConfiguration(conf);
    newConf.setInt(CommonConfigurationKeysPublic.IPC_CLIENT_CONNECT_MAX_RETRIES_ON_SOCKET_TIMEOUTS_KEY,
            conf.getInt(YarnConfiguration.RM_NODEMANAGER_CONNECT_RETRIES,
                    YarnConfiguration.DEFAULT_RM_NODEMANAGER_CONNECT_RETRIES));
    setConfig(newConf);
    super.serviceInit(newConf);
}

From source file:org.apache.cxf.systest.jaxrs.JAXRSContinuationsTest.java

private void doTestContinuation(String pathSegment) throws Exception {
    ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(10));
    CountDownLatch startSignal = new CountDownLatch(1);
    CountDownLatch doneSignal = new CountDownLatch(5);

    executor.execute(new BookWorker("http://localhost:" + PORT + "/bookstore/" + pathSegment + "/1", "1",
            "CXF in Action1", startSignal, doneSignal));
    executor.execute(new BookWorker("http://localhost:" + PORT + "/bookstore/" + pathSegment + "/2", "2",
            "CXF in Action2", startSignal, doneSignal));
    executor.execute(new BookWorker("http://localhost:" + PORT + "/bookstore/" + pathSegment + "/3", "3",
            "CXF in Action3", startSignal, doneSignal));
    executor.execute(new BookWorker("http://localhost:" + PORT + "/bookstore/" + pathSegment + "/4", "4",
            "CXF in Action4", startSignal, doneSignal));
    executor.execute(new BookWorker("http://localhost:" + PORT + "/bookstore/" + pathSegment + "/5", "5",
            "CXF in Action5", startSignal, doneSignal));

    startSignal.countDown();//from  w  ww.  j a v  a2 s . co  m
    doneSignal.await(60, TimeUnit.SECONDS);
    executor.shutdownNow();
    assertEquals("Not all invocations have completed", 0, doneSignal.getCount());
}

From source file:org.apache.hadoop.mapred.gridmix.JobSubmitter.java

/**
 * Initialize the submission component with downstream monitor and pool of
 * files from which split data may be read.
 * @param monitor Monitor component to which jobs should be passed
 * @param threads Number of submission threads
 *   See {@link Gridmix#GRIDMIX_SUB_THR}.
 * @param queueDepth Max depth of pending work queue
 *   See {@link Gridmix#GRIDMIX_QUE_DEP}.
 * @param inputDir Set of files from which split data may be mined for
 * synthetic job//from   w ww .  j a v  a 2 s  .  c  o  m
 * @param statistics
 */
public JobSubmitter(JobMonitor monitor, int threads, int queueDepth, FilePool inputDir, Statistics statistics) {
    sem = new Semaphore(queueDepth);
    sched = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>());
    this.inputDir = inputDir;
    this.monitor = monitor;
    this.statistics = statistics;
}

From source file:com.splout.db.dnode.CustomTThreadPoolServer.java

public CustomTThreadPoolServer(Args args) {
    super(args);// ww  w .  j a v a  2s. c om

    LinkedBlockingQueue<Runnable> executorQueue = new LinkedBlockingQueue<Runnable>(args.maxWorkerThreads);

    stopTimeoutUnit = args.stopTimeoutUnit;
    stopTimeoutVal = args.stopTimeoutVal;

    executorService_ = new ThreadPoolExecutor(args.minWorkerThreads, args.maxWorkerThreads, 60,
            TimeUnit.SECONDS, executorQueue);
}

From source file:org.htrace.impl.LocalFileSpanReceiver.java

@Override
public void configure(HTraceConfiguration conf) {
    this.executorTerminationTimeoutDuration = EXECUTOR_TERMINATION_TIMEOUT_DURATION_DEFAULT;
    int capacity = conf.getInt(CAPACITY_KEY, CAPACITY_DEFAULT);
    this.file = conf.get(PATH_KEY);
    if (file == null || file.isEmpty()) {
        throw new IllegalArgumentException("must configure " + PATH_KEY);
    }//from  ww w . j av  a2s.c  om
    this.executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(capacity));
    try {
        this.fwriter = new FileWriter(this.file, true);
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
    this.bwriter = new BufferedWriter(fwriter);
    this.values = new LinkedHashMap<String, Object>();
}

From source file:org.bimserver.tools.ifcloader.BulkLoader.java

private void start() {
    Path basePath = Paths.get("C:\\Bulk");
    Path bulkPath = basePath.resolve("bulk");
    Path regularPath = basePath.resolve("single");
    try (JsonBimServerClientFactory factory = new JsonBimServerClientFactory("http://localhost:8080")) {
        ExecutorService executorService = new ThreadPoolExecutor(16, 16, 1, TimeUnit.HOURS,
                new ArrayBlockingQueue<>(10000));
        try (BimServerClient client = factory
                .create(new UsernamePasswordAuthenticationInfo("admin@bimserver.org", "admin"))) {
            if (Files.exists(bulkPath)) {
                DirectoryStream<Path> stream = Files.newDirectoryStream(bulkPath);
                for (Path path : stream) {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                SProject project = client.getServiceInterface()
                                        .addProject(path.getFileName().toString(), "ifc2x3tc1");
                                client.bulkCheckin(project.getOid(), path, "Automatic bulk checkin");
                            } catch (ServerException e) {
                                e.printStackTrace();
                            } catch (UserException e) {
                                e.printStackTrace();
                            } catch (PublicInterfaceNotFoundException e) {
                                e.printStackTrace();
                            }//w  w  w .ja v  a  2s.  c om
                        }
                    });
                }
            }
            if (Files.exists(regularPath)) {
                DirectoryStream<Path> regularStream = Files.newDirectoryStream(regularPath);
                for (Path regularFile : regularStream) {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            String filename = regularFile.getFileName().toString().toLowerCase();
                            try {
                                if (filename.endsWith(".ifc") || filename.endsWith(".ifczip")) {
                                    String schema = client.getServiceInterface().determineIfcVersion(
                                            extractHead(regularFile),
                                            filename.toLowerCase().endsWith(".ifczip"));
                                    SProject project = client.getServiceInterface().addProject(filename,
                                            schema);
                                    SDeserializerPluginConfiguration deserializer = client.getServiceInterface()
                                            .getSuggestedDeserializerForExtension("ifc", project.getOid());
                                    client.checkinSync(project.getOid(), "Automatic checkin",
                                            deserializer.getOid(), false, regularFile);
                                } else {
                                    LOGGER.info("Skipping " + filename);
                                }
                            } catch (Exception e) {
                                LOGGER.error(filename, e);
                            }
                        }
                    });
                }
            }
            executorService.shutdown();
            executorService.awaitTermination(24, TimeUnit.HOURS);
        }
    } catch (BimServerClientException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.dbay.apns4j.impl.ApnsServiceImpl.java

private ApnsServiceImpl(ApnsConfig config, ErrorProcessHandler errorProcessHandler)
        throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException,
        CertificateException, CertificateExpiredException, IOException {
    this.name = config.getName();
    int poolSize = config.getPoolSize();
    // this.service = Executors.newFixedThreadPool(poolSize);
    this.service = new ThreadPoolExecutor(poolSize, poolSize, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(queueSize));

    SocketFactory factory = ApnsTools.createSocketFactory(config.getKeyStore(), config.getPassword(),
            KEYSTORE_TYPE, ALGORITHM, PROTOCOL);
    this.connPool = ApnsConnectionPool.newConnPool(config, factory, errorProcessHandler);
    this.feedbackConn = new ApnsFeedbackConnectionImpl(config, factory);
}