Example usage for java.util.concurrent ThreadPoolExecutor getActiveCount

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

Introduction

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

Prototype

public int getActiveCount() 

Source Link

Document

Returns the approximate number of threads that are actively executing tasks.

Usage

From source file:org.paxle.filter.robots.impl.RobotsTxtManager.java

/**
 * {@inheritDoc}/*from www. jav  a  2  s .  c o m*/
 * @see org.osgi.service.monitor.Monitorable#getStatusVariable(String)
 */
public StatusVariable getStatusVariable(String id) throws IllegalArgumentException {
    if (!VAR_NAMES.contains(id)) {
        throw new IllegalArgumentException("Invalid Status Variable name " + id);
    }

    int val = 0;
    int type = StatusVariable.CM_GAUGE;

    if (id.equals(MONITOR_STORE_SIZE)) {
        val = this.loader.size();
    } else if (id.startsWith(MONITOR_JOBS_PREFIX)) {
        ThreadPoolExecutor execService = this.execService;
        if (id.equals(MONITOR_JOBS_ACTIVE)) {
            val = execService.getActiveCount();
        } else if (id.equals(MONITOR_JOBS_IDLE)) {
            long max = execService.getMaximumPoolSize();
            long active = execService.getActiveCount();
            val = (int) (max - active);
        } else if (id.equals(MONITOR_JOBS_MAX)) {
            val = execService.getMaximumPoolSize();
        } else if (id.equals(MONITOR_JOBS_PENDING)) {
            long enqued = execService.getTaskCount();
            long total = execService.getCompletedTaskCount();
            long active = execService.getActiveCount();
            val = (int) (enqued - total - active);
        } else if (id.equals(MONITOR_JOBS_TOTAL)) {
            val = (int) execService.getCompletedTaskCount();
            type = StatusVariable.CM_CC;
        }
    }

    return new StatusVariable(id, type, val);
}

From source file:org.polymap.core.runtime.UnboundPoolExecutor.java

public static ExecutorService newInstance() {
    final int procs = Runtime.getRuntime().availableProcessors();
    final int maxThreads = procs * MAX_THREADS_PER_PROC;

    // thread factory
    ThreadFactory threadFactory = new ThreadFactory() {
        volatile int threadNumber = 0;

        public Thread newThread(Runnable r) {
            String prefix = "polymap-";
            Thread t = new Thread(r, prefix + threadNumber++);
            t.setDaemon(false);/*from w  w  w . j  av  a  2s  .c o  m*/
            t.setPriority(DEFAULT_THREAD_PRIORITY);
            return t;
        }
    };

    // thread pool
    ThreadPoolExecutor executor = new ThreadPoolExecutor(procs, maxThreads, 180L, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>(), threadFactory);

    // rejected? -> wait and try again
    executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
        Random rand = new Random();

        public void rejectedExecution(Runnable r, ThreadPoolExecutor _executor) {
            do {
                try {
                    Thread.sleep(rand.nextInt(1000) + 100);
                } catch (InterruptedException e) {
                }
            } while (_executor.getActiveCount() >= maxThreads);

            _executor.execute(r);
        }
    });

    //executor.allowCoreThreadTimeOut( true );        
    return executor;
}