- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为一些非常简单的事情而苦苦挣扎......
我的真实项目多年来一直受到未知问题的困扰,他们决定创建一个非常简单的测试,结果让我感到害怕......
这是测试:
ExecutorService t = new ThreadPoolExecutor(10, 20, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(600));
for (int i = 0; i < 100; i++) {
final int i1 = i;
t.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println(i1);
try {
Thread.sleep(5000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
我正在创建一个线程池,其中包含 10 个核心线程和 20 个 maximumPoolSize
然后我给它 100 个线程,每个线程将简单地打印一个固定数字...
我谦虚愚蠢的想法是:
The pool has 10 threads, will print 0-9 randomly then after some instans 10 extra threads will be created and pool will print from 0-19 randomly
这对我来说很明显,因为 maxSize 是 20,在最坏的情况下它应该接受 20 个任务......
但结果是0-9一直打印
问题是:如果永远不会安排额外的线程执行,maximumPoolSize
的意义何在?
最佳答案
正如@Oleg 正确指出的那样,池工作正常,但有一个我不知道的实现细节。
只有当任务队列已满时才会创建额外的线程
这篇文章解释得更好: http://www.bigsoft.co.uk/blog/2009/11/27/rules-of-a-threadpoolexecutor-pool-size
Take this example. Starting thread pool size is 1, core pool size is 5, max pool size is 10 and the queue is 100.
Sun's way: as requests come in threads will be created up to 5, then tasks will be added to the queue until it reaches 100. When the queue is full new threads will be created up to maxPoolSize. Once all the threads are in use and the queue is full tasks will be rejected. As the queue reduces so does the number of active threads.
User anticipated way: as requests come in threads will be created up to 10, then tasks will be added to the queue until it reaches 100 at which point they are rejected. The number of threads will rename at max until the queue is empty. When the queue is empty the threads will die off until there are corePoolSize left.
所以 Java 会等到队列爆满前的最后一秒来创建新线程...
关于java - java中ThreadPoolExecutor maximumPoolSize的作用是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53919084/
这是正确的吗? ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor)
我只是对我编写的一些代码感到非常困惑。我惊讶地发现: with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
我正在尝试同时使用 InheritableThreadLocal 和 ThreadPoolExecutor。 这是因为 ThreadPoolExecutor 为每个池重用线程(毕竟它是一个池),这意味
concurrent.futures.ThreadPoolExecutor 通过 传递 function 到执行器 executor.submit(my_function) 像这样: def my_f
将一个简单的测试程序放在一起,应该并行执行一些任务。每次我们提交6个任务,等待完成。然后,又提交了一组任务。 import java.util.concurrent.*; public class
我正在运行一段 python 代码,其中多个线程通过线程池执行程序运行。每个线程都应该执行一项任务(例如获取网页)。我想要做的是终止所有线程,即使其中一个线程失败。例如: with ThreadPoo
我有一个使用阻塞队列的ThreadPoolExecutor,并且正在尝试调试一个问题,在该问题中,我怀疑任务在ThreadPoolExecutor的队列中停留的时间太长,无法执行。我正在尝试验证这一理
我正在尝试使用 futures backport 包在 Python 中使用 ThreadPoolExecutor。然而,问题是所有线程都是同时执行的,所以没有实际的池化发生。更具体地说,我得到了该函
我有两个 list : a = [1, 2, 3, 4] b = [9, 8, 7, 6] 我希望将这两个列表的每个组合作为参数传递给我正在执行多线程处理的函数: def test(hello, wo
当我们谈论ThreadPoolExecutor时,核心池大小和最大池大小之间到底有什么区别? 可以用例子来解释吗? 最佳答案 来自this blog post : Take this example.
我对“concurrent.futures”的并行处理相当陌生,我正在测试一些简单的实验。我编写的代码似乎可以工作,但我不确定如何存储结果。我尝试创建一个列表(“ future ”)并将结果附加到该列
我审查了多线程,并尝试实现一个创建单独线程来运行收集进程的应用程序。该过程中的主要方法需要一个变量数组列表,我正在尝试找出一种将数组列表传递给每个线程的方法。 ApplicationContext c
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我需要读取一个大的 csv 文件(328 MB)并对其进行处理。每行的处理还包括调用 Web 服务。 我是第一次使用ThreadPoolExecutor。我的逻辑是,我将从 csv 中每 100 行吐
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('I
我有一个带有 run 方法的类,该类的 main 方法中的计时器正在使用以下代码调用该类: Timer timer = new Timer(); timer.scheduleAtFixedRate(n
尝试调试竞争条件,其中我们的应用程序的轮询器线程之一永远不会返回,导致 future 的轮询器永远不会被调度。用抽象术语来说,在捕获问题时隐藏我们的业务逻辑,这就是我们的代码路径。 我们必须更新远程服
下面的错误是什么意思?我怎样才能恢复它? Exception in thread "UserActionProcessor-8811" java.util.concurrent.RejectedExe
我正在致力于增强现有的 Java 应用程序。该应用程序是一个消息处理器,每天处理数百万条消息。它基本上是使用 Core Java 编写的,线程和队列是使用 Collection 类实现的。 在此应用程
我想更新使用用户定义线程池的旧代码。我想使用 java ThreadPoolExecutor,但问题是发送到线程池的请求不是可运行的。有什么方法可以将 ThreadPoolExecutor 与现有请求
我是一名优秀的程序员,十分优秀!