java 中 ThreadPoolExecutor maximumPoolSize 的作用是什么
what is the function of ThreadPoolExecutor maximumPoolSize in java
我正在为一些非常简单的事情而苦苦挣扎...
我的真实项目多年来一直受到未知问题的困扰,他们决定创建一个非常简单的测试,结果让我感到害怕...
这是测试:
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 等到你的队列爆裂之前的最后一秒来创建一个新线程...
是的,根据@Oleg 和您尝试的实现:
new ThreadPoolExecutor(10, 20, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue(600));
这里,10 是 corePoolSize - 意味着 Jvm 将为前 10 个任务的新任务创建新线程。其他任务将添加到队列中,直到队列满(600 个任务)。
20 是 maxPoolSize - JVM 最多可以创建 20 个线程。意味着如果已经有 10 个 task/thread 是 运行ning 并且队列已满,有 600 个待处理任务,如果又有一个新的 request/task 到达队列,那么 JVM 将创建最多 20 个的新线程(线程总数=前 10 个 + 新 10 个);
new ArrayBlockingQueue(600) = 是总队列大小 - 它可以在其中排队 600 个任务。
一旦所有 20 个线程 运行ning 并且如果有新任务到达,则该新任务将被拒绝。
[来自文档]
SUN内部线程创建规则:
- 如果线程数小于corePoolSize,则创建一个新的Thread来运行一个新任务。
- 如果线程数等于(或大于)corePoolSize,则将任务放入队列。
- 如果队列已满,并且线程数小于maxPoolSize,创建一个新线程到运行个任务中。
- 如果队列已满,且线程数大于或等于maxPoolSize,则拒绝任务。
希望对您有所帮助。
我正在为一些非常简单的事情而苦苦挣扎...
我的真实项目多年来一直受到未知问题的困扰,他们决定创建一个非常简单的测试,结果让我感到害怕...
这是测试:
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 等到你的队列爆裂之前的最后一秒来创建一个新线程...
是的,根据@Oleg 和您尝试的实现:
new ThreadPoolExecutor(10, 20, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue(600));
这里,10 是 corePoolSize - 意味着 Jvm 将为前 10 个任务的新任务创建新线程。其他任务将添加到队列中,直到队列满(600 个任务)。
20 是 maxPoolSize - JVM 最多可以创建 20 个线程。意味着如果已经有 10 个 task/thread 是 运行ning 并且队列已满,有 600 个待处理任务,如果又有一个新的 request/task 到达队列,那么 JVM 将创建最多 20 个的新线程(线程总数=前 10 个 + 新 10 个);
new ArrayBlockingQueue(600) = 是总队列大小 - 它可以在其中排队 600 个任务。
一旦所有 20 个线程 运行ning 并且如果有新任务到达,则该新任务将被拒绝。
[来自文档] SUN内部线程创建规则:
- 如果线程数小于corePoolSize,则创建一个新的Thread来运行一个新任务。
- 如果线程数等于(或大于)corePoolSize,则将任务放入队列。
- 如果队列已满,并且线程数小于maxPoolSize,创建一个新线程到运行个任务中。
- 如果队列已满,且线程数大于或等于maxPoolSize,则拒绝任务。
希望对您有所帮助。