- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有一个任务队列,其初始化如下所示:
LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(maximumPoolSize, maximumPoolSize, 50000L, TimeUnit.MILLISECONDS, queue);
maximumPoolSize
的值为 200。在工作时,队列会获得大量线程(超过一千个),但 threadPoolExecutor.getActiveCount() 返回的值
始终小于或等于 100。例如,值 threadPoolExecutor.getActiveCount()
和 queue.size()
记录如下:
logger.debug("Active threads: " + threadPoolExecutor.getActiveCount() + ". The queue has " + queue.size() + " threads.");
结果我们得到以下日志:
Active threads: 1. The queue has 0 threads.
Active threads: 2. The queue has 0 threads.
Active threads: 3. The queue has 0 threads.
Active threads: 4. The queue has 0 threads.
Active threads: 5. The queue has 0 threads.
...
Active threads: 86. The queue has 0 threads.
Active threads: 87. The queue has 1 threads.
Active threads: 88. The queue has 1 threads.
Active threads: 89. The queue has 1 threads.
Active threads: 90. The queue has 1 threads.
...
Active threads: 99. The queue has 1 threads.
Active threads: 100. The queue has 1 threads.
Active threads: 100. The queue has 2 threads.
Active threads: 100. The queue has 3 threads.
Active threads: 100. The queue has 4 threads.
...
Active threads: 100. The queue has 1874 threads.
Active threads: 100. The queue has 1875 threads.
Active threads: 100. The queue has 1876 threads.
Active threads: 100. The queue has 1877 threads.
Active threads: 100. The queue has 1878 threads.
Active threads: 100. The queue has 1879 threads.
Active threads: 100. The queue has 1880 threads.
Active threads: 100. The queue has 1881 threads.
Active threads: 100. The queue has 1882 threads.
Active threads: 100. The queue has 1883 threads.
文档说 threadPoolExecutor.getActiveCount()
方法
returns the approximate number of threads that are actively executing tasks
但是为什么这个近似值的最大阈值总是maximumPoolSize
/2?应该是这样吗?
附注我无法重现这种情况:在这种情况下,我的 PC 上总是有 200 个 Activity 线程。但上面的日志不是来 self 的电脑。它可以取决于处理器/虚拟核心的数量吗?
我还在 The Grey Blog 中发现了一段有趣的代码:
int cpus = Runtime.getRuntime().availableProcessors();
int maxThreads = cpus * scaleFactor;
maxThreads = (maxThreads > 0 ? maxThreads : 1);
但最终我很困惑,因为 Runtime.getRuntime().availableProcessors()
的值在我的电脑上是 8。
最佳答案
您确定变量 maximumPoolSize
设置为 200
。在我这边工作得很好。
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class SO35570728 {
private class Task implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " started");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " finished");
}
}
public static void main(String[] args) {
LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(200, 200, 50000L, TimeUnit.MILLISECONDS, queue);
for(int i= 0 ; i < 100000 ; i++){
System.out.println("Active threads: " + threadPoolExecutor.getActiveCount() + ". The queue has " + queue.size() + " threads.");
threadPoolExecutor.submit(new SO35570728().new Task());
}
}
}
输出
Active threads: 0. The queue has 0 threads.
Active threads: 1. The queue has 0 threads.
Active threads: 2. The queue has 0 threads.
Active threads: 3. The queue has 0 threads.
...
pool-1-thread-1 started
pool-1-thread-2 started
pool-1-thread-3 started
...
Active threads: 200. The queue has 99793 threads.
Active threads: 200. The queue has 99794 threads.
Active threads: 200. The queue has 99795 threads.
Active threads: 200. The queue has 99796 threads.
Active threads: 200. The queue has 99797 threads.
Active threads: 200. The queue has 99798 threads.
Active threads: 200. The queue has 99799 threads.
关于java - 为什么总是 threadPoolExecutor.getActiveCount() <= MaximumPoolSize/2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35570728/
我有一个 ThreadPoolExecutor,当我调用 getActiveCount() 时它似乎在欺骗我。然而,我没有做过很多多线程编程,所以也许我做错了什么。 这是我的 TPE @Overrid
ThreadPoolExecutor#getActiveCount() 的 javadocs假设该方法“返回正在执行任务的线程的大致数量。” 是什么让这个数字是近似值而不是精确值?它会多报还是少报 A
我们有一个任务队列,其初始化如下所示: LinkedBlockingQueue queue = new LinkedBlockingQueue(); ThreadPoolExecutor thread
ThreadPoolExecutor getActiveCount() 为 1 但 getQueue 返回空队列,为什么? 我有一个执行器,其类型是ThreadPoolExecutor。 我调用 ge
我是一名优秀的程序员,十分优秀!