简体   繁体   English

从执行程序服务池(JAVA)中查找等待线程的作业数?

[英]Find number of jobs waiting for thread from executor service pool (JAVA)?

I have a server that has multiple worker threads implemented using java executorService (ie. a thread pool)我有一个服务器,它有多个使用 java executorService 实现的工作线程(即线程池)

My problem is that I am not able to log at every second, the length of jobs waiting to be processed if there is no idle thread available from the thread pool.我的问题是,如果线程池中没有可用的空闲线程,我无法每秒记录等待处理的作业长度。

NOTE: Logging is not my problem but I want to be able to see how many tasks/jobs are waiting to be processed by a worker thread, is there anyway to see the length of the waiting queue (not the thread pool ) inside executor service?注意:日志记录不是我的问题,但我希望能够看到有多少任务/作业正在等待工作线程处理,是否可以查看执行程序服务中等待队列(不是线程池)的长度?

I have no idea how to implement this thing.我不知道如何实现这个东西。

The ThreadPoolExecutor constructor accepts a BlockingQueue parameter which is the Queue implementation used to store the waiting jobs. ThreadPoolExecutor构造函数接受一个BlockingQueue参数,该参数是用于存储等待作业的Queue实现。 You can request this queue using getQueue() method, then check the size of the queue: 您可以使用getQueue()方法请求此队列,然后检查队列的大小:

System.out.println("Number of waiting jobs: "+executor.getQueue().size());

Note that this method is not available in the ExecutorService interface, thus it's better to construct the ThreadPoolExecutor explicitly instead of using Executors.newFixedThreadPool and friends: 请注意,此方法在ExecutorService接口中不可用,因此最好显式构造ThreadPoolExecutor而不要使用Executors.newFixedThreadPool和好友:

ThreadPoolExecutor executor = new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());

While Executors.newFixedThreadPool in OpenJDK/OracleJDK does the same, it's not specified, thus using (ThreadPoolExecutor)Executors.newFixedThreadPool(nThreads) may cause ClassCastException in future Java versions or alternative JDK implementations. 尽管OpenJDK / OracleJDK中的Executors.newFixedThreadPool相同,但未指定,因此使用(ThreadPoolExecutor)Executors.newFixedThreadPool(nThreads)可能会在将来的Java版本或替代JDK实现中导致ClassCastException

If you can assume that the ExecutorService implementation used by your server is ThreadPoolExecutor , then you can use the method getQueue() that returns the number of tasks that have not been assigned to a Worker yet. 如果可以假设服务器使用的ExecutorService实现是ThreadPoolExecutor ,则可以使用方法getQueue()返回尚未分配给Worker的任务数。

/**
 * Returns the task queue used by this executor. Access to the
 * task queue is intended primarily for debugging and monitoring.
 * This queue may be in active use.  Retrieving the task queue
 * does not prevent queued tasks from executing.
 *
 * @return the task queue
 */
public BlockingQueue<Runnable> getQueue() {
    return workQueue;
}

So you can run something like this: 因此,您可以运行以下内容:

 if(LOGGER.isDebugEnabled()) {
    LOGGER.debug(String.format("Pending tasks: %d", executor.getQueue().size()));
 }

Just as a suggestion use ThreadPoolExecutor instead of ExecutorService. 就像建议一样,请使用ThreadPoolExecutor而不是ExecutorService。 You can take advantage of the blocking queue present in the ThreadPoolExecutor class. 您可以利用ThreadPoolExecutor类中存在的阻塞队列。 This would give you the count of threads waiting. 这将为您提供等待线程的数量。

Also ThreadPoolExecutor class is having methods to get the count of submitted tasks and executed task. ThreadPoolExecutor类还具有一些方法来获取已提交任务和已执行任务的计数。

Refer the 请参阅

ThreadPoolExecutor 线程池执行器

BlockingQueue 阻塞队列

Hope this helps 希望这可以帮助

I'd suggest keeping a counter of the tasks added to the pool and a counter of the tasks completed/started by the pool.我建议保留添加到池中的任务的计数器和池完成/启动的任务的计数器。 Simple and works with any threading paradigm.简单并且适用于任何线程范例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM