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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33343674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:32:15  来源:igfitidea点击:

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

javamultithreading

提问by LoveMeow

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.

我不知道如何实现这个东西。

回答by Tagir Valeev

The ThreadPoolExecutorconstructor accepts a BlockingQueueparameter which is the Queueimplementation used to store the waiting jobs. You can request this queue using getQueue()method, then check the size of the queue:

ThreadPoolExecutor构造函数接受一个BlockingQueue参数,它是Queue用来存储等待处理的作业执行。您可以使用getQueue()方法请求此队列,然后检查队列的大小:

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

Note that this method is not available in the ExecutorServiceinterface, thus it's better to construct the ThreadPoolExecutorexplicitly instead of using Executors.newFixedThreadPooland friends:

请注意,此方法在ExecutorService接口中不可用,因此最好ThreadPoolExecutor显式构造而不是使用Executors.newFixedThreadPool和朋友:

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

While Executors.newFixedThreadPoolin OpenJDK/OracleJDK does the same, it's not specified, thus using (ThreadPoolExecutor)Executors.newFixedThreadPool(nThreads)may cause ClassCastExceptionin future Java versions or alternative JDK implementations.

虽然Executors.newFixedThreadPool在 OpenJDK/OracleJDK 中也做同样的事情,但没有指定,因此在未来的 Java 版本或替代的 JDK 实现中使用(ThreadPoolExecutor)Executors.newFixedThreadPool(nThreads)可能会导致ClassCastException

回答by jfcorugedo

If you can assume that the ExecutorServiceimplementation 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 Workeryet.

如果您可以假设ExecutorService您的服务器使用的实现是ThreadPoolExecutor,那么您可以使用getQueue()返回尚未分配给 a 的任务数的方法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()));
 }

回答by Vivek Singh

Just as a suggestion use ThreadPoolExecutor instead of ExecutorService. You can take advantage of the blocking queue present in the ThreadPoolExecutor class. This would give you the count of threads waiting.

正如建议使用 ThreadPoolExecutor 而不是 ExecutorService。您可以利用 ThreadPoolExecutor 类中存在的阻塞队列。这将为您提供等待的线程数。

Also ThreadPoolExecutor class is having methods to get the count of submitted tasks and executed task.

此外 ThreadPoolExecutor 类具有获取提交任务和执行任务计数的方法。

Refer the

参考

ThreadPoolExecutor

线程池执行器

BlockingQueue

阻塞队列

Hope this helps

希望这可以帮助