Tomcat Thread pool optimization

Source: Internet
Author: User

Brief introduction

The thread pool is widely used as a scheme to improve the ability of process processing data. A large number of servers are more or less used in the thread pool technology, whether in Java or C + + implementation, the thread pool has the following characteristics:

A thread pool typically has three important parameters:

    1. maximum number of threads. The total number of threads does not exceed this number at any time when the program is running. If the number of requests exceeds the maximum number, it waits for the other threads to finish before processing.

    2. worker thread. The worker thread is primarily run execution code, and there are two states: idle and run state. In an idle state, it is similar to "hibernate", Waiting for a task, while processing the running state indicates that the task is running (Runnable).

    3. worker thread. It is primarily responsible for monitoring the status of the thread pool: whether the idle thread exceeds the maximum number of idle threads or less than the minimum number of idle threads. If the requirements are not met, adjust them.

What's the matter with a thread pool? In fact, the thread pool principle is very simple, similar to the concept of buffer in the operating system, its flow is as follows: Start a number of threads, and let these threads are asleep, when the client has a new request, will wake up a thread pool of a sleep thread, let it handle the client's request, When the request is processed, the thread is asleep again. Maybe you might ask: why bother if I create a new thread whenever the client has a new request? This may be a good idea because it makes it easier for you to write code, but you ignore an important question?? Performance! Take my unit, my unit is a centralized data center of the bank network, the peak of the client requests per second more than 100 concurrent, if you create a new thread for each client request, then the CPU time and memory will be amazing, if the use of a thread pool with 200 threads, That would save a lot of system resources, allowing more CPU time and memory to handle real business applications, rather than frequent thread creation and destruction.

Configuration

Using a thread pool to handle more access with fewer threads can improve the ability of Tomcat to process requests. How to use:

First of all. Open/conf/server.xml, add

  1. <executor name nameprefix "catalina-exec-"

  2. MaxThreads = "a"

  3. minsparethreads = "a"

  4. MaxIdleTime = "60000"

  5. prestartminsparethreads = "true"

  6. maxqueuesize = " the" />

Name: Thread names

Nameprefix: Thread Prefix

MaxThreads: The maximum number of concurrent connections, not configured by default 200, generally recommended to set 500~ 800, according to their own hardware facilities and actual business needs.

Minsparethreads:tomcat the number of threads starting to initialize, default value 25

Prestartminsparethreads: Initializes the value of Minsparethreads when Tomcat is initialized and does not set true Minsparethreads

Maxqueuesize: Maximum number of waiting queues, exceeding deny request

Minsparethreads: Thread maximum idle time 60 seconds.

Then, modify the node to increase the Executor property, such as:

  1. <connectorport="8080"protocol=" Org.apache.coyote.http11.Http11Nio2Protocol "

  2. ConnectionTimeout = "20000"

  3. Redirectport = "8443"

  4. Executor = "Tomcatthreadpool"

  5. enablelookups = "false"

  6. Acceptcount = " the"

  7. maxpostsize = "10485760"

  8. Compression = "on"

  9. Disableuploadtimeout = "true"

  10. compressionminsize = "2048"

  11. nocompressionuseragents = "Gozilla, Traviata"

  12. Acceptorthreadcount = "2"

  13. Compressablemimetype = "Text/html,text/xml,text/plain,text/css,text/java,application/java"

  14. uriencoding = "Utf-8" />

Port: Connect ports.

Protocol: The mode of transmission used by the connector. Tomcat 8 Settings Nio2 better: Org.apache.coyote.http11.Http11Nio2Protocol

Protocol, Tomcat 6, 7 set NiO better: Org.apache.coyote.http11.Http11NioProtocol

Note:

Each Web client request is a separate thread for the server side, and the increase in the number of requests from the client will cause the number of threads to go up, and the CPU will be busy switching to the thread.

NIO uses single-threaded (single CPU) or only a small number of multithreaded (multi-CPU) to accept sockets, and the thread pool handles requests that clog the pipe or queue. In this case, the Web server can handle the request as long as the OS can accept the TCP connection. Greatly improves the scalability of the Web server.

Executor: The thread pool name used by the connector

Enablelookups: Disabling DNS queries

Acceptcount: Specifies the number of requests that can be placed into the processing queue when all the threads that can be used to process the request are used, and requests that exceed this number will not be processed, with the default setting of 100.

Maxpostsize: Limit the content size of the POST request as a form URL parameter, in bytes, by default is 2097152 (2 trillion), and 10485760 is 10M. If you want to disable throttling, you can set it to-1.

Acceptorthreadcount: The number of threads used to receive connections, the default value is 1. Generally this refers to the need to change the time is because the server is a multi-core CPU, if the multi-core CPU is generally configured to 2.

Compression: compression is transmitted.

Compressionminsize: Size of compression

Nocompressionuseragents: Browser not enabled for compression

Tips:

Compression increases the Tomcat burden, preferably with Nginx + tomcat or Apache + tomcat, compressed to Nginx/apache.

Tomcat compression is after the client requests the server corresponding resources, from the server side of the resource file compression, and then output to the client, by the client's browser is responsible for extracting and browsing. It saves around 40% of traffic relative to the normal browsing process, HTML, CSS, Java, and text. More importantly, it can be dynamically generated, including CGI, PHP, JSP, ASP, servlet,shtml and other output of the Web page can also be compressed, compression efficiency is very high.


This article is from the "Dream to Reality" blog, please be sure to keep this source http://lookingdream.blog.51cto.com/5177800/1975246

Tomcat Thread pool optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.