Example usage for java.util.concurrent ThreadPoolExecutor getCorePoolSize

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

Introduction

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

Prototype

public int getCorePoolSize() 

Source Link

Document

Returns the core number of threads.

Usage

From source file:Main.java

/**
 * /*ww w . ja va 2s  .  c om*/
 */
protected static void decorateProperties(ThreadPoolExecutor executor, Properties properties) {
    if (executor != null) {
        if (properties != null) {
            properties.setProperty("thread-keep-alive-time",
                    Long.toString(executor.getKeepAliveTime(TimeUnit.MILLISECONDS)));

            properties.setProperty("thread-pool-size-largest", Integer.toString(executor.getLargestPoolSize()));

            properties.setProperty("thread-pool-size-minimum", Integer.toString(executor.getCorePoolSize()));

            properties.setProperty("thread-pool-size-maximum", Integer.toString(executor.getMaximumPoolSize()));

            properties.setProperty("thread-pool-size", Integer.toString(executor.getPoolSize()));

            properties.setProperty("runnable-completed-count", Long.toString(executor.getCompletedTaskCount()));

            properties.setProperty("runnable-active-count", Integer.toString(executor.getActiveCount()));

            properties.setProperty("queue-size", Integer.toString(executor.getQueue().size()));

            properties.setProperty("queue-capacity-remaining",
                    Integer.toString(executor.getQueue().remainingCapacity()));
        }
    }
}

From source file:eu.cassandra.sim.utilities.Utils.java

public static void printExecutorSummary(ThreadPoolExecutor executor) {
    System.out.println(String.format(
            "[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",
            executor.getPoolSize(), executor.getCorePoolSize(), executor.getActiveCount(),
            executor.getCompletedTaskCount(), executor.getTaskCount(), executor.isShutdown(),
            executor.isTerminated()));//from www .j a  v a 2 s .  c o  m
}

From source file:code.google.nfs.rpc.grizzly.server.GrizzlyServer.java

public void start(int listenPort, ExecutorService threadpool) throws Exception {
    ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) threadpool;
    ThreadPoolConfig config = ThreadPoolConfig.defaultConfig().copy()
            .setCorePoolSize(threadPoolExecutor.getCorePoolSize())
            .setMaxPoolSize(threadPoolExecutor.getMaximumPoolSize()).setPoolName("GRIZZLY-SERVER");
    ExecutorService executorService = GrizzlyExecutorService.createInstance(config);

    FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless();
    filterChainBuilder.add(new TransportFilter());
    filterChainBuilder.add(new GrizzlyProtocolFilter());
    filterChainBuilder.add(new GrizzlyServerHandler(executorService));
    TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance();
    builder.setOptimizedForMultiplexing(true);
    builder.setIOStrategy(SameThreadIOStrategy.getInstance());

    transport = builder.build();// w w w .ja  va  2 s.  c om

    transport.setProcessor(filterChainBuilder.build());
    transport.bind(listenPort);

    transport.start();
    LOGGER.warn("server started,listen at: " + listenPort);
}

From source file:com.alibaba.otter.node.etl.OtterController.java

public int getThreadPoolSize() {
    if (executorService instanceof ThreadPoolExecutor) {
        ThreadPoolExecutor pool = (ThreadPoolExecutor) executorService;
        return pool.getCorePoolSize();
    }//from   w ww . j  a  va  2s . c o  m

    return 0;
}

From source file:com.redhat.example.rules.runtimestats.DefaultRuleSimulator.java

public void setJittingThreads(int jittingThreads) {
    this.jittingThreads = jittingThreads;
    int n = jittingThreads > 0 ? jittingThreads : Runtime.getRuntime().availableProcessors();
    ThreadPoolExecutor ex = (ThreadPoolExecutor) ExecutorProviderFactory.getExecutorProvider().getExecutor();
    if (n != ex.getCorePoolSize()) {
        ex.setCorePoolSize(n);/*w  ww . j  av a  2  s  .  c  o  m*/
        ex.setMaximumPoolSize(n);
    }
}

From source file:org.jmangos.commons.threadpool.CommonThreadPoolManager.java

/**
 * @see org.jmangos.commons.threadpool.ThreadPoolManager#fillPoolStats(org.jmangos.commons.threadpool.model.ThreadPoolType)
 *///w ww. j av  a  2  s  .  co m
@Override
public PoolStats fillPoolStats(final ThreadPoolType poolType) {

    ThreadPoolExecutor executor = null;
    switch (poolType) {
    case INSTANT:
        executor = this.instantPool;
        break;
    case SCHEDULED:
    default:
        executor = this.scheduledPool;
        break;
    }
    final PoolStats stats = new PoolStats(poolType);
    stats.setActiveCount(executor.getActiveCount());
    stats.setCompletedTaskCount(executor.getCompletedTaskCount());
    stats.setCorePoolSize(executor.getCorePoolSize());
    stats.setLargestPoolSize(executor.getLargestPoolSize());
    stats.setMaximumPoolSize(executor.getMaximumPoolSize());
    stats.setPoolSize(executor.getPoolSize());
    stats.setQueueSize(executor.getQueue().size());
    stats.setTaskCount(executor.getTaskCount());
    return stats;
}

From source file:org.apache.hadoop.mapreduce.v2.app.launcher.TestContainerLauncher.java

@Test(timeout = 10000)
public void testPoolSize() throws InterruptedException {

    ApplicationId appId = ApplicationId.newInstance(12345, 67);
    ApplicationAttemptId appAttemptId = ApplicationAttemptId.newInstance(appId, 3);
    JobId jobId = MRBuilderUtils.newJobId(appId, 8);
    TaskId taskId = MRBuilderUtils.newTaskId(jobId, 9, TaskType.MAP);

    AppContext context = mock(AppContext.class);
    CustomContainerLauncher containerLauncher = new CustomContainerLauncher(context);
    containerLauncher.init(new Configuration());
    containerLauncher.start();/*from  ww w. ja va2  s .c o m*/

    ThreadPoolExecutor threadPool = containerLauncher.getThreadPool();

    // No events yet
    Assert.assertEquals(containerLauncher.initialPoolSize,
            MRJobConfig.DEFAULT_MR_AM_CONTAINERLAUNCHER_THREADPOOL_INITIAL_SIZE);
    Assert.assertEquals(0, threadPool.getPoolSize());
    Assert.assertEquals(containerLauncher.initialPoolSize, threadPool.getCorePoolSize());
    Assert.assertNull(containerLauncher.foundErrors);

    containerLauncher.expectedCorePoolSize = containerLauncher.initialPoolSize;
    for (int i = 0; i < 10; i++) {
        ContainerId containerId = ContainerId.newContainerId(appAttemptId, i);
        TaskAttemptId taskAttemptId = MRBuilderUtils.newTaskAttemptId(taskId, i);
        containerLauncher.handle(new ContainerLauncherEvent(taskAttemptId, containerId, "host" + i + ":1234",
                null, ContainerLauncher.EventType.CONTAINER_REMOTE_LAUNCH));
    }
    waitForEvents(containerLauncher, 10);
    Assert.assertEquals(10, threadPool.getPoolSize());
    Assert.assertNull(containerLauncher.foundErrors);

    // Same set of hosts, so no change
    containerLauncher.finishEventHandling = true;
    int timeOut = 0;
    while (containerLauncher.numEventsProcessed.get() < 10 && timeOut++ < 200) {
        LOG.info("Waiting for number of events processed to become " + 10 + ". It is now "
                + containerLauncher.numEventsProcessed.get() + ". Timeout is " + timeOut);
        Thread.sleep(1000);
    }
    Assert.assertEquals(10, containerLauncher.numEventsProcessed.get());
    containerLauncher.finishEventHandling = false;
    for (int i = 0; i < 10; i++) {
        ContainerId containerId = ContainerId.newContainerId(appAttemptId, i + 10);
        TaskAttemptId taskAttemptId = MRBuilderUtils.newTaskAttemptId(taskId, i + 10);
        containerLauncher.handle(new ContainerLauncherEvent(taskAttemptId, containerId, "host" + i + ":1234",
                null, ContainerLauncher.EventType.CONTAINER_REMOTE_LAUNCH));
    }
    waitForEvents(containerLauncher, 20);
    Assert.assertEquals(10, threadPool.getPoolSize());
    Assert.assertNull(containerLauncher.foundErrors);

    // Different hosts, there should be an increase in core-thread-pool size to
    // 21(11hosts+10buffer)
    // Core pool size should be 21 but the live pool size should be only 11.
    containerLauncher.expectedCorePoolSize = 11 + containerLauncher.initialPoolSize;
    containerLauncher.finishEventHandling = false;
    ContainerId containerId = ContainerId.newContainerId(appAttemptId, 21);
    TaskAttemptId taskAttemptId = MRBuilderUtils.newTaskAttemptId(taskId, 21);
    containerLauncher.handle(new ContainerLauncherEvent(taskAttemptId, containerId, "host11:1234", null,
            ContainerLauncher.EventType.CONTAINER_REMOTE_LAUNCH));
    waitForEvents(containerLauncher, 21);
    Assert.assertEquals(11, threadPool.getPoolSize());
    Assert.assertNull(containerLauncher.foundErrors);

    containerLauncher.stop();

    // change configuration MR_AM_CONTAINERLAUNCHER_THREADPOOL_INITIAL_SIZE
    // and verify initialPoolSize value.
    Configuration conf = new Configuration();
    conf.setInt(MRJobConfig.MR_AM_CONTAINERLAUNCHER_THREADPOOL_INITIAL_SIZE, 20);
    containerLauncher = new CustomContainerLauncher(context);
    containerLauncher.init(conf);
    Assert.assertEquals(containerLauncher.initialPoolSize, 20);
}

From source file:org.geotools.gce.imagemosaic.ImageMosaicReader.java

/**
 * Constructor.// www.j  ava  2  s  . co  m
 * 
 * @param source
 *            The source object.
 * @throws IOException
 * @throws UnsupportedEncodingException
 * 
 */
public ImageMosaicReader(Object source, Hints uHints) throws IOException {
    super(source, uHints);

    //
    // try to extract a multithreaded loader if available
    //
    if (this.hints.containsKey(Hints.EXECUTOR_SERVICE)) {
        final Object executor = uHints.get(Hints.EXECUTOR_SERVICE);
        if (executor != null && executor instanceof ExecutorService) {
            multiThreadedLoader = (ExecutorService) executor;
            if (LOGGER.isLoggable(Level.FINE)) {
                if (multiThreadedLoader instanceof ThreadPoolExecutor) {
                    final ThreadPoolExecutor tpe = (ThreadPoolExecutor) multiThreadedLoader;
                    LOGGER.fine("Using ThreadPoolExecutor with the following settings: " + "core pool size = "
                            + tpe.getCorePoolSize() + "\nmax pool size = " + tpe.getMaximumPoolSize()
                            + "\nkeep alive time " + tpe.getKeepAliveTime(TimeUnit.MILLISECONDS));
                }
            }
        }
    }
    if (this.hints.containsKey(Hints.MAX_ALLOWED_TILES))
        this.maxAllowedTiles = ((Integer) this.hints.get(Hints.MAX_ALLOWED_TILES));

    //
    // Check source
    //
    if (source instanceof ImageMosaicDescriptor) {
        initReaderFromDescriptor((ImageMosaicDescriptor) source, uHints);
    } else {
        try {
            initReaderFromURL(source, uHints);
        } catch (Exception e) {
            throw new DataSourceException(e);
        }
    }
}

From source file:org.jumpmind.symmetric.service.impl.NodeCommunicationService.java

protected ThreadPoolExecutor getExecutor(final CommunicationType communicationType) {
    ThreadPoolExecutor service = executors.get(communicationType);

    String threadCountParameter = "";
    switch (communicationType) {
    case PULL:/*  ww w  .  jav  a  2s . c  o m*/
        threadCountParameter = ParameterConstants.PULL_THREAD_COUNT_PER_SERVER;
        break;
    case PUSH:
        threadCountParameter = ParameterConstants.PUSH_THREAD_COUNT_PER_SERVER;
        break;
    case FILE_PULL:
        threadCountParameter = ParameterConstants.FILE_PUSH_THREAD_COUNT_PER_SERVER;
        break;
    case FILE_PUSH:
        threadCountParameter = ParameterConstants.FILE_PUSH_THREAD_COUNT_PER_SERVER;
        break;
    case EXTRACT:
        threadCountParameter = ParameterConstants.INITIAL_LOAD_EXTRACT_THREAD_COUNT_PER_SERVER;
        break;
    default:
        break;
    }
    int threadCount = parameterService.getInt(threadCountParameter, 1);

    if (service != null && service.getCorePoolSize() != threadCount) {
        log.info("{} has changed from {} to {}.  Restarting thread pool",
                new Object[] { threadCountParameter, service.getCorePoolSize(), threadCount });
        stop();
        service = null;
    }

    if (service == null) {
        synchronized (this) {
            service = executors.get(communicationType);
            if (service == null) {
                if (threadCount <= 0) {
                    log.warn("{}={} is not a valid value. Defaulting to 1", threadCountParameter, threadCount);
                    threadCount = 1;
                } else if (threadCount > 1) {
                    log.info("{} will use {} threads", communicationType.name().toLowerCase(), threadCount);
                }
                service = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadCount, new ThreadFactory() {
                    final AtomicInteger threadNumber = new AtomicInteger(1);
                    final String namePrefix = parameterService.getEngineName().toLowerCase() + "-"
                            + communicationType.name().toLowerCase() + "-";

                    public Thread newThread(Runnable r) {
                        Thread t = new Thread(r);
                        t.setName(namePrefix + threadNumber.getAndIncrement());
                        if (t.isDaemon()) {
                            t.setDaemon(false);
                        }
                        if (t.getPriority() != Thread.NORM_PRIORITY) {
                            t.setPriority(Thread.NORM_PRIORITY);
                        }
                        return t;
                    }
                });
                executors.put(communicationType, service);
            }
        }
    }
    return service;
}