Introduction to implementation methods of several open-source Java Web Container thread pools-Tomcat (I)

Source: Internet
Author: User

Currently, open-source Java Web containers commonly used in the market include Tomcat, Resin, and Jetty. Resin needs to be purchased from V3.0 for commercial purposes, while the other two are pure open-source. You can download the latest Binary Package and source code from their websites.

As a Web container, it must withstand high traffic volumes and respond to requests from different users at the same time. It can maintain high stability and robustness in harsh environments. In the HTTP server field, ApacheHTTPD is the most efficient and stable, but it can only process static page requests. To support dynamic page requests, you must install the corresponding plug-ins, for example, mod_perl can process Perl scripts, while mod_python can process Python scripts.

The Web containers mentioned above are all HTTP servers written in Java. Of course, they can be embedded in Apache or used independently. Analysis of the methods for processing client requests helps you understand the implementation methods of Java multithreading and thread pools, and lay a solid foundation for designing a powerful multi-threaded server.

Tomcat is the most widely used Java Web Container with powerful functions and high scalability. To improve response speed and efficiency, the latest version of Tomcat (5.5.17) uses Apache Portable Runtime (APR) as the bottom layer and uses various technologies such as Socket and buffer pool in APR, performance is also improved. APR is also the bottom layer of Apache HTTPD. It can be imagined that there are still a lot of mutual use cases for members of the ASF (Apache Software Foundation), although different development languages are used.

Tomcat's thread pool is located in the tomcat-util.jar file and contains two thread pool schemes. Solution 1: Use APR Pool technology and JNI; solution 2: Use ThreadPool implemented by Java. The second type is introduced here. If you want to know APR's Pool technology, you can view the source code of APR.

By default, ThreadPool creates five threads and stores them in a 200-dimension thread array. These threads are started when they are created. Of course, when there is no request, they all process the "Waiting" status (in fact, it is a while loop, constantly waiting for Y ). If there is a request, the idle thread will be awakened to execute the user's request.

The specific request process is: when the service is started, create a one-dimensional thread array (maxThread = 200), and create Idle threads (minSpareThreads = 5) to wait for user requests at any time. When a user requests, call the threadpool. runIt (ThreadPoolRunnable) method to send an instance to ThreadPool. The instance to be executed must implement the ThreadPoolRunnable interface. ThreadPool first searches for Idle threads. If there are Idle threads, it runs to execute ThreadPoolRunnable. If there is no idle thread and it does not exceed maxThreads, it creates minSpareThreads Idle threads at a time. If maxThreads is exceeded, wait for the idle thread. In short, you need to find a idle thread to use it to execute the instance. Find the thread and remove it from the thread array. Then wake up the idle thread that has been found and use it to run the instance ThreadPoolRunnable ). After running ThreadPoolRunnable, place the thread in the thread array and use it as an idle thread for subsequent use.

It can be seen that the implementation of Tomcat's thread pool is relatively simple, and ThreadPool. java only has 840 lines of code. Use a one-dimensional array to save Idle threads, and create Idle threads at a small pace (5) each time and put them in the thread pool. Remove Idle threads from the array during use. after use, return them to the thread pool.

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.