У нас есть очередь задач и ее инициализация выглядит так:
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()
возвращает приблизительное количество потоков, активно выполняющих задачи
Но почему максимальный порог этого приблизительного значения всегда равен maximumPoolSize
/2? Это должно быть так?
P.S. Я не мог воспроизвести эту ситуацию: на моем ПК в этом случае всегда было 200 активных потоков. Но приведенные выше журналы не с моего компьютера. Может это зависит от количества процессоров/виртуальных ядер?
Я также нашел интересный фрагмент кода в Серый блог< /а>:
int cpus = Runtime.getRuntime().availableProcessors();
int maxThreads = cpus * scaleFactor;
maxThreads = (maxThreads > 0 ? maxThreads : 1);
Но в конце концов я очень запутался, потому что значение Runtime.getRuntime().availableProcessors()
на моем ПК равно 8.
threadPoolExecutor.getPoolSize()
Максимум не зависит от количества ядер, которые у вас есть, а только от точки, в которой не рекомендуется иметь больше. - person Peter Lawrey   schedule 23.02.2016threadPoolExecutor.getMaximumPoolSize()
, чтобы убедиться, чтоmaximumPoolSize
действительно 200. - person Roman   schedule 26.02.2016