Configuring Thread Pools for Java Web Servers – 为Java Web服务器配置线程池

最后修改: 2020年 2月 21日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we take a look at thread pool configuration for Java web application servers such as Apache Tomcat, Glassfish Server, and Oracle Weblogic.

在本教程中,我们看一下Java网络应用服务器的线程池配置,如Apache Tomcat、Glassfish Server和Oracle Weblogic。

2. Server Thread Pools

2.服务器线程池

Server thread pools are used and managed by a web application server for a deployed application. These thread pools exist outside of the web container or servlet so they are not subject to the same context boundary.

服务器线程池由Web应用服务器为已部署的应用程序使用和管理。这些线程池存在于Web容器或Servlet之外,因此它们不受同一上下文边界的约束。

Unlike application threads, server threads exist even after a deployed application is stopped.

与应用程序线程不同,服务器线程甚至在部署的应用程序停止后仍然存在。

3. Apache Tomcat

3.Apache Tomcat

First, we can configure Tomcat’s server thread pool via the Executor configuration class in our server.xml:

首先,我们可以通过server.xml中的Executor配置类配置Tomcat的服务器线程池。

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="25"/>

minSpareThreads is the smallest the pool will be, including at startup. maxThreads is the largest the pool will be before the server starts queueing up requests.

minSpareThreads是池子的最小值,包括启动时。maxThreads是指在服务器开始排队等待请求之前,池子的最大容量

Tomcat defaults these to 25 and 200, respectively. In this configuration, we’ve made the thread pool a bit smaller than the default.

Tomcat的默认值分别为25和200。在这个配置中,我们使线程池比默认值小一点。

3.1. Embedded Tomcat

3.1.嵌入式Tomcat

Similarly, we can alter an embedded Tomcat server for Spring Boot to configure a thread pool by setting an application property:

同样,我们可以改变Spring Boot的嵌入式Tomcat服务器,通过设置应用程序属性来配置线程池。

server.tomcat.max-threads=250

Starting with Boot 2.3, the property has changed to:

从Boot 2.3开始,该属性已改为。

server.tomcat.threads.max=250

4. Glassfish

4.Glassfish

Next, let’s update our Glassfish server.

接下来,让我们更新我们的Glassfish服务器。

Glassfish uses an admin command in contrast to Tomcat’s XML configuration file, server.xml. From the prompt, we run:

Glassfish使用了一个管理命令,与Tomcat的XML配置文件server.xml相反。从提示符中,我们运行。

create-threadpool

We can add to create-threadpool the flags maxthreadpoolsize and minthreadpoolsize. They function similarly to Tomcat minSpareThreads and maxThreads:

我们可以向create-threadpool添加标志maxthreadpoolsizeminthreadpoolsize。它们的功能类似于Tomcat的minSpareThreadsmaxThreads

--maxthreadpoolsize 250 --minthreadpoolsize 25

We can also specify how long a thread can be idle before returning to the pool:

我们还可以指定一个线程在返回池子之前可以空闲多长时间。

--idletimeout=2

And then, we supply the name of our thread pool at the end:

然后,我们在最后提供我们的线程池的名称。

asadmin> create-threadpool --maxthreadpoolsize 250 --minthreadpoolsize 25 --idletimeout=2 threadpool-1

5. Weblogic

5.Weblogic

Oracle Weblogic gives us the ability to alter a self-tuning thread pool with a WorkManager.

Oracle Weblogic为我们提供了用WorkManager改变自调谐线程池的能力。

Similarly to thread queues, a WorkManager manages a thread pool as a queue. However, the WorkManager adds dynamic threads based on real-time throughput. Weblogic performs analysis on throughput regularly to optimize thread utilization.

与线程队列类似,WorkManager将线程池作为一个队列来管理。然而,WorkManager会根据实时吞吐量来增加动态线程。Weblogic会定期对吞吐量进行分析,以优化线程利用率。

What does this mean for us? It means that while we may alter the thread pool, the web server will ultimately decide on whether to spawn new threads.

这对我们来说意味着什么呢?这意味着,虽然我们可以改变线程池,但网络服务器将最终决定是否产生新的线程。

We can configure our thread pool in the Weblogic Admin Console:

我们可以在Weblogic Admin Console中配置我们的线程池。

Updating the Self Tuning Minimum Thread Pool Size and Self Tuning Thread Maximum Pool Size values set the min and max boundaries for the WorkManagers.

更新Self Tuning Minimum Thread Pool SizeSelf Tuning Thread Maximum Pool Size值为WorkManagers设置最小和最大界限。

Notice the Stuck Thread Max Time and Stuck Thread Timer Interval values. These help the WorkManager classify stuck threads.

注意Stuck Thread Max TimeStuck Thread Timer Interval的值。这些有助于WorkManager对卡住的线程进行分类。

Sometimes a long-running process may cause a build-up of stuck threads. The WorkManager will spawn new threads from the thread pool to compensate. Any update to these values could prolong the time to allow the process to finish.

有时,一个长期运行的进程可能会导致卡住的线程堆积。WorkManager将从线程池中生成新的线程来进行补偿。对这些值的任何更新都可能延长让进程完成的时间。

Stuck threads could be indicative of code problems, so it’s always best to address the root cause rather than use a workaround.

故障线程可能是代码问题的指示,因此最好是解决根本原因,而不是使用变通方法。

6. Conclusion

6.结语

In this quick article, we looked at multiple ways to configure application server thread pools.

在这篇快速文章中,我们研究了配置应用服务器线程池的多种方法。

While there are differences in how the application servers manage the various thread pools, they are configured using similar concepts.

虽然应用服务器管理各种线程池的方式存在差异,但它们的配置采用了类似的概念。

Finally, let’s remember that changing configuration values for web servers are not appropriate fixes for poor performing code and bad application designs.

最后,让我们记住,改变网络服务器的配置值并不是对性能差的代码和糟糕的应用设计的适当修复。