[英]How to get the number of tasks in a queue in executor service?
所以我使用executorservice来创建一个线程池。
ExecutorService executor = Executors.newSingleThreadExecutor();
我试图访问线程池队列中的任务数。 我看到没有方法可以做到这一点。 我知道有一些方法可以在threadpoolexecutor中获取队列大小,但是如何使用executorservice对象来实现这一点。
就像我说的,如果我创建了像这样的ThreadpoolExecutor,我可以获取队列信息
ThreadPoolExecutor tpExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
我知道我可以使用tpExecutor.queue.size()来获取threadpool队列中的任务数。 但是目前我已经使用Executor Service声明了我的线程池。 我怎样才能做到这一点? 如果人们可以编写代码并进行演示,那将是值得注意的。
我认为这应该有效:
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) this.executorService;
int activeCount = threadPoolExecutor.getActiveCount();
long taskCount = threadPoolExecutor.getTaskCount();
long completedTaskCount = threadPoolExecutor.getCompletedTaskCount();
long tasksToDo = taskCount - completedTaskCount - activeCount;
您可以将其强制转换为ThreadPoolExecutor。
ThreadPoolExecutor ex =(ThreadPoolExecutor)executor; ex.getQueue()大小();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.