gpt4 book ai didi

java - 如何理解 "add up how much of that resource each task requires and divide that into the total quantity available"决定线程池大小

转载 作者:行者123 更新时间:2023-11-30 02:14:13 25 4
gpt4 key购买 nike

我正在学习 Java 并发实践,第 8.2 章:调整线程池大小。

作者声称,通过将任务所需的所有资源的数量除以可用资源的数量,我们得到了线程池大小的上限。

我无法理解这一点。根据他的理论,如果我们的任务总共需要 4GB 内存,而我们有 8GB 可用内存,那么线程池大小的上限将为 0.5??

引用原文段落:

Of course, CPU cycles are not the only resource you might want to manage using thread pools. Other resources that can contribute to sizing constraints are memory, file handles, socket handles, and database connections. Calculating pool size constraints for these types of resources is easier: just add up how much of that resource each task requires and divide that into the total quantity available. The result will be an upper bound on the pool size.

最佳答案

你很接近,但你的等式本质上是颠倒的。

假设我们有 10 GB 可用内存,每个线程最多需要 1 GB 内存。

在没有任何方程的情况下,我们可以推断最多只能有 10 个线程,因为这总共将使用 10 GB 内存。

我们可以用来计算的方程是:

              Total Quantity Available   10 GB
# Of Tasks = ------------------------ = ----- = 10
Resources Per Task 1 GB

关于java - 如何理解 "add up how much of that resource each task requires and divide that into the total quantity available"决定线程池大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49102836/

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