Example usage for java.util.concurrent ThreadPoolExecutor getQueue

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

Introduction

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

Prototype

public BlockingQueue<Runnable> getQueue() 

Source Link

Document

Returns the task queue used by this executor.

Usage

From source file:org.wso2.carbon.analytics.iots.smarthomeanalytics.SensorAgentService.java

public static void initialization() {
    try {/*from w w  w  .ja va 2 s  .c om*/
        dataPublisher = new DataPublisher(
                Constants.TRASPORT_LEVEL_PROTOCOL + Constants.SERVER_IP_ADDRESS + ":" + Constants.SERVER_PORT,
                Constants.USERNAME, Constants.PASSWORD);
        if (executorService == null) {
            RejectedExecutionHandler rejectedExecutionHandler = new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    try {
                        executor.getQueue().put(r);
                    } catch (InterruptedException e) {
                        log.error("Exception while adding event to executor queue : " + e.getMessage(), e);
                    }
                }
            };
            executorService = new ThreadPoolExecutor(Constants.ADAPTER_MIN_THREAD_POOL_SIZE,
                    Constants.ADAPTER_MAX_THREAD_POOL_SIZE, Constants.DEFAULT_KEEP_ALIVE_TIME_IN_MILLS,
                    TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>(Constants.ADAPTER_EXECUTOR_JOB_QUEUE_SIZE),
                    rejectedExecutionHandler);
        }
    } catch (DataEndpointConfigurationException e) {
        log.error("Required fields are missing in Data endpoint configuration file ", e);
    } catch (DataEndpointException e) {
        log.error("Error while connecting to configured endpoint ", e);
    } catch (DataEndpointAgentConfigurationException e) {
        log.error("Required fields are missing in Data endpoint agent configuration file ", e);
    } catch (TransportException e) {
        log.error("Error while connecting to server through Thrift", e);
    } catch (DataEndpointAuthenticationException e) {
        log.error("Please check whether user name and password is correct,", e);
    }
}

From source file:com.sm.store.Utils.java

/**
 * Check if the thread pool is overload or not
 * @param pools/* w w w  . ja  v a  2  s . co  m*/
 * @return true or false
 */
public static boolean isOverLoaded(ThreadPoolExecutor pools) {
    int remain = pools.getQueue().remainingCapacity();
    int active = pools.getActiveCount();
    if (remain < active) {
        logger.info("Remaining capacity " + remain + " is less than active threads " + active);
        return true;
    } else
        return false;
}

From source file:com.basho.riak.presto.cli.CLI.java

private static void waitForQueueSizeLessThan(ThreadPoolExecutor executor, int size)
        throws InterruptedException {

    while (executor.getQueue().size() > size) {
        Thread.sleep(10L);//from w  ww .ja v  a2 s . c  o  m
    }
}

From source file:com.sm.transport.Utils.java

public static boolean isOverLoaded(ThreadPoolExecutor pools) {
    int remain = pools.getQueue().remainingCapacity();
    int active = pools.getActiveCount();
    if (remain < active) {
        logger.info("Remaining capacity " + remain + " is less than active threads " + active);
        return true;
    } else/*from ww w .ja  va 2  s.co  m*/
        return false;
}

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

private static int getTaskCount(ThreadPoolExecutor[] threadPools) {
    int result = 0;

    for (ThreadPoolExecutor threadPool : threadPools)
        result += threadPool.getQueue().size() + threadPool.getActiveCount();

    return result;
}

From source file:com.bosscs.spark.commons.utils.Utils.java

/**
 * Returns an instance of ThreadPoolExecutor using an bounded queue and blocking when the worker queue is full.
 * @param nThreads thread pool size/*from   w ww .  j  a va  2s .  c  o  m*/
 * @param queueSize workers queue size
 * @return thread pool executor
 */
public static ExecutorService newBlockingFixedThreadPoolExecutor(int nThreads, int queueSize) {
    BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(queueSize);
    RejectedExecutionHandler blockingRejectedExecutionHandler = new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {
            try {
                executor.getQueue().put(task);
            } catch (InterruptedException e) {
            }
        }

    };

    return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, blockingQueue,
            blockingRejectedExecutionHandler);
}

From source file:com.clustercontrol.plugin.impl.AsyncWorkerPlugin.java

public static int getTaskCount(String worker) throws HinemosUnknown {
    ThreadPoolExecutor executor = _executorMap.get(worker);

    if (executor == null) {
        throw new HinemosUnknown("worker thread is not initialized. (worker = " + worker + ")");
    }/*w  w w  .j  av a2 s .c  o m*/

    synchronized (_executorLock.get(worker)) {
        return executor.getQueue().size();
    }
}

From source file:org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder.java

/**
 * @deprecated This method is only used from configuration modules and thus callers of it
 *             should use service injection to make the executor configurable.
 *//*from   w w w. j  a  va2 s . c  o  m*/
@Deprecated
public static synchronized ListeningExecutorService getDefaultNotificationExecutor() {

    if (NOTIFICATION_EXECUTOR == null) {
        int queueSize = MAX_NOTIFICATION_QUEUE_SIZE;
        final String queueValue = System.getProperty(NOTIFICATION_QUEUE_SIZE_PROPERTY);
        if (StringUtils.isNotBlank(queueValue)) {
            try {
                queueSize = Integer.parseInt(queueValue);
                logger.trace("Queue size was set to {}", queueSize);
            } catch (final NumberFormatException e) {
                logger.warn("Cannot parse {} as set by {}, using default {}", queueValue,
                        NOTIFICATION_QUEUE_SIZE_PROPERTY, queueSize);
            }
        }

        // Overriding the queue:
        // ThreadPoolExecutor would not create new threads if the queue is not full, thus adding
        // occurs in RejectedExecutionHandler.
        // This impl saturates threadpool first, then queue. When both are full caller will get blocked.
        final BlockingQueue<Runnable> delegate = new LinkedBlockingQueue<>(queueSize);
        final BlockingQueue<Runnable> queue = new ForwardingBlockingQueue<Runnable>() {
            @Override
            protected BlockingQueue<Runnable> delegate() {
                return delegate;
            }

            @Override
            public boolean offer(final Runnable r) {
                // ThreadPoolExecutor will spawn a new thread after core size is reached only
                // if the queue.offer returns false.
                return false;
            }
        };

        final ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true)
                .setNameFormat("md-sal-binding-notification-%d").build();

        final ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_NOTIFICATION_THREADS,
                MAX_NOTIFICATION_THREADS, NOTIFICATION_THREAD_LIFE, TimeUnit.SECONDS, queue, factory,
                new RejectedExecutionHandler() {
                    // if the max threads are met, then it will raise a rejectedExecution. We then push to the queue.
                    @Override
                    public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
                        try {
                            executor.getQueue().put(r);
                        } catch (final InterruptedException e) {
                            throw new RejectedExecutionException("Interrupted while waiting on the queue", e);
                        }
                    }
                });

        NOTIFICATION_EXECUTOR = MoreExecutors.listeningDecorator(executor);
    }

    return NOTIFICATION_EXECUTOR;
}

From source file:Main.java

/**
 * /* w  w w .  j  av  a2s  .co m*/
 */
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:com.espertech.esperio.db.core.ExecutorServices.java

public BlockingQueue<Runnable> getQueue(String name) {
    if (!services.containsKey(name)) {
        return null;
    }//w w w .ja v  a 2 s. co m
    ThreadPoolExecutor executor = (ThreadPoolExecutor) services.get(name);
    return executor.getQueue();
}