gpt4 book ai didi

java - ThreadPoolExecutor getActiveCount() 为 1 但 getQueue 返回空队列,为什么?

转载 作者:行者123 更新时间:2023-12-02 04:17:21 25 4
gpt4 key购买 nike

ThreadPoolExecutor getActiveCount() 为 1 但 getQueue 返回空队列,为什么?

我有一个执行器,其类型是ThreadPoolExecutor。

我调用 getActiveCount() 并得到 1,

但是当我调用 getQueue.size() 时,它是 0。

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html#getQueue%28%29

我的代码是这样的。

  System.out.println(taskExecutor.getActiveCount());
System.out.println(taskExecutor.getThreadPoolExecutor().getQueue().size());

所以我很困惑,为什么这两个不一样?

最佳答案

根据javadocs(重点是我的)

getActiveCount():返回正在主动执行任务的大约线程数。

getQueue():返回此执行器使用的任务队列

现在解释一下... getQueue() 返回任务正在等待执行的队列,一旦任务准备好执行,就会从队列并交给线程。另外,请记住 getActive() 返回一个近似值,不应在任何类型的逻辑中使用。

因此您的问题有 2 个可能的答案。有一个任务正在运行...或者 getActive() 返回一个近似值。不管怎样,没有任务等待执行。

关于java - ThreadPoolExecutor getActiveCount() 为 1 但 getQueue 返回空队列,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33177010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com