- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
How to get the ThreadPoolExecutor to increase threads to max before queueing?
(10 个回答)
3年前关闭。
在 线程池执行器 类,有一个 maxPoolSize 来指定最大线程池大小。这意味着如果线程数小于该数量,则应立即执行池中的线程。但我发现事实并非如此。它实际上不能超出 corePoolSize。我很迷惑。如果什么都不做,maxPoolSize 的目的是什么?这是我的测试程序:
我已经指定了 corePoolSize = 2;最大池大小 = 6;我创建了 5 个线程(可运行)。我认为所有 5 个线程(Runnable)都应该同时运行。但他们不是。其中只有两个在运行,另外三个放在一边,直到前两个死亡。
我已经阅读了很多关于这个主题的帖子。但是没有人可以指导我让 5 个线程同时运行。
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class MyThreadPoolExecutorTest2
{
private List<MyRunnable> myRunnables = new ArrayList<>();
public static void main(String[] args)
{
new MyThreadPoolExecutorTest2().test();
}
public void test()
{
int poolSize = 2;
int maxPoolSize = 6;
int threadPoolKeepAliveTimeInSec = 30;
ExecutorService threadPoolExecutor =
new MySimpleThreadPoolExecutor(poolSize, maxPoolSize, threadPoolKeepAliveTimeInSec);
int numOfThread = 5;
System.out.println("Start thread pool test with corePoolSize=" + poolSize + ", maxPoolSize=" + maxPoolSize
+ ", actualThreads=" + numOfThread);
for (int i = 0; i < numOfThread; i++)
{
MyRunnable tempRunnable = new MyRunnable(i + 1, "PoolTest" + (i + 1));
myRunnables.add(tempRunnable);
threadPoolExecutor.execute(tempRunnable);
}
System.out.println("********* wait for a while");
try
{
Thread.sleep(20000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("^^^^^^^^^^ shutdown them all");
for (MyRunnable runnable : myRunnables)
{
runnable.shutdown();
}
System.out.println("Ended thread pool test.");
}
public class MyRunnable implements Runnable
{
private int id = 0;
private String name = "";
private boolean shutdown = false;
public MyRunnable(int id, String name)
{
this.id = id;
this.name = name;
}
@Override
public void run()
{
System.out.println("++++ Starting Thread: " + id + ":" + name);
while (!shutdown)
{
try
{
Thread.sleep(200);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("---- Ended Thread: " + id + ":" + name);
}
public void shutdown()
{
shutdown = true;
}
}
}
class MySimpleThreadPoolExecutor extends ThreadPoolExecutor
{
private static int peakActiveThreads = 0;
private String taskInfo = "";
public MySimpleThreadPoolExecutor(int nThreads, int maxThreads, int threadPoolKeepAliveTimeInSec)
{
super(nThreads, maxThreads, threadPoolKeepAliveTimeInSec * 1000L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
System.out.println("MySimpleThreadPoolExecutor::MySimpleThreadPoolExecutor(), threadPoolSize=" + nThreads
+ ", maxThreadCount=" + maxThreads + ", threadPoolKeepAliveTimeInSec=" + threadPoolKeepAliveTimeInSec);
}
@Override
public void beforeExecute(Thread t, Runnable r)
{
int activeCount = getActiveCount();
if (MySimpleThreadPoolExecutor.peakActiveThreads < activeCount)
{
MySimpleThreadPoolExecutor.peakActiveThreads = activeCount;
}
taskInfo = r.toString();
String msg =
"BeforeE thread(name:id)::" + t.getName() + ":" + t.getId() + ", task::" + r.toString() + "\n"
+ threadPoolInfoStr();
System.out.println("ThreadInfo before, MySimpleThreadPoolExecutor::beforeExecute(), " + msg);
super.beforeExecute(t, r);
}
@Override
public void execute(Runnable command)
{
beforeExecute(Thread.currentThread(), command);
super.execute(command);
}
public String threadPoolInfoStr()
{
return String.format("Thead: %s/%d\n[PoolSize/CorePoolSize] [%d/%d]\nActive: %d\nCompleted: %d\nTask: %d"
+ "\nisShutdown: %s\nisTerminated: %s\npeakActiveThreads: %d\nTaskInfo: %s\nQueueSize: %d", Thread
.currentThread().getName(), Thread.currentThread().getId(), getPoolSize(), getCorePoolSize(),
getActiveCount(), getCompletedTaskCount(), getTaskCount(), isShutdown(), isTerminated(),
MySimpleThreadPoolExecutor.peakActiveThreads, taskInfo, getQueue().size());
}
}
最佳答案
一旦队列已满,新线程只会被创建到 maxPoolSize。之前,限制是在 corePoolSize 中定义的那个。
引用:http://www.bigsoft.co.uk/blog/index.php/2009/11/27/rules-of-a-threadpoolexecutor-pool-size
关于java - 为什么 ThreadPoolExecutor 中的 maxPoolSize 什么都不做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47971745/
我已经设置了自定义线程配置文件,我可以看到从以下配置创建了 20 个线程。 但我从来没有看到线程数增加到 20 以上,但最大池大小配置为 50。什么时候使用最大池大小? 我们尝试对 50 个具有并发
我使用 HikariConfig 作为 spring 服务器上 postgres 数据库的数据源。我应该设置 maxPoolSize 吗? (默认值为 -1)我可以使用多少池大小?是否与硬件有任何依赖
在连接到数据库时,我使用连接池和 Firebird 数据库。我使用 FirebirdSql.Data.FirebirdClient 版本 2.6.5.0。我有以下连接字符串: 我有以下使用数据库连接
我将 MaxPoolSize 值增加到 3000。这是否意味着允许使用此实例的并发连接数为 3000,或者它也计算通过其他对象实例的任何其他连接数? var connectionString = "m
我遇到了可怕的错误: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool
我作为初学者正在学习java中的线程和并发包,并且我已经阅读了有关 ThreadPoolExecutor 的文档以了解 getPoolSize() 和 getCorePoolSize() 之间的区别,
这个问题在这里已经有了答案: How to get the ThreadPoolExecutor to increase threads to max before queueing? (10 个回答
我们有一个托管在 RDS 中的 MySQl 数据库。创建 MySQL 连接时,似乎未应用最大池大小。我们有一个看起来像这样的连接字符串 Server=myServer;Port=3306;Databa
我的应用程序中有 8 个 Verticle。每个 Verticle 都在一个单独的线程上。每个 Verticle 都有一个 WebClient(Vert.x HTTP 客户端)我将 MaxPoolSi
我已将 Tomcat 配置为使用 ComboPooledDataSource,方法是在我的 context.xml 中添加以下内容。 我想将数据库连接数限制为 20,如 maxPoolSize 中所
我试图找出我们服务中一些随机、间歇性“卡住”的根本原因,在排除了几个问题后,我们为我们的应用程序打开了 c3p0 调试级别的日志记录,我们认为这似乎是一个 c3p0 配置问题。我们使用“Oracle
我的申请是 Piwik Server从放置在数百个网站上的跟踪代码接收传入的跟踪数据。当这些跟踪请求进入时,大部分工作负载是每秒向数据库写入数百次。我使用的是带有 JDBC 和 Hibernate 的
当使用 golang 连接到 mongodb 实例时,我们可以指定 maxPoolSize 的值通过 connection string或使用 ClientOptions.SetMaxPoolSize
所以我有两个 bash 脚本。一个使用“scala”命令调用字节码,另一个使用“java”命令调用相同的代码。我的问题如下,当我使用 scala 时,我可以看到我可以获得大约 80 个线程(我创建并显
我必须向网站的所有用户发送大量电子邮件。我想为每封发送的电子邮件使用一个线程池。目前我已将值设置为: 两者之间有什么区别,是否会扩展。目前我有大约。 10000 个用户。 最佳答案 以下是 Sun
我在vertx java中创建了一个java后端服务。我使用 httpClient(io.vertx.core.http.HttpClient) 并启用连接池来连接到外部服务。我排除吞吐量为 50。对
我有一个 SwingWorker 可以获取 URL 列表(可能有数百个)。在 doInBackground() 方法中,它循环遍历这些 URL,每次创建一个 CloseableHttpClient,由
executor.setCorePoolSize(5); executor.setMaxPoolSize(5); 我在 Spring 中有一个 UI 应用程序和后端。一种方法需要 15 秒才能执行。我
如何在 mongodb-php 驱动程序版本 1.2.0 中使用 maxPoolSize。 新的 mongodb php 驱动程序是否使用连接池,如果是那么如何更改它? 最佳答案 你可以使用 Mong
我是一名优秀的程序员,十分优秀!