Introduction to the Tomcat thread pool implementation (i)

Source: Internet
Author: User
Open source Java commonly used in the market at present Web ContainerThere are Tomcat, resin and Jetty. Of these, resin need to be purchased from V3.0 for commercial purposes, while the other two are purely open source. You can download the latest binaries and source code from their Web sites, respectively.

As a Web container , it is necessary to withstand high traffic, to be able to respond to requests from different users at the same time, to maintain high stability and robustness in harsh environments. In the HTTP server domain, Apache httpd is the most efficient and stable, but it can only handle static page requests, if you need to support dynamic page requests, you must install the appropriate plug-ins, such as Mod_perl can handle Perl scripts, Mod_ Python can handle Python scripts.

The three web containers described above are HTTP servers written in Java, and they can all be embedded in Apache and can be used independently. Analyzing how they handle customer requests helps to understand how Java multithreading and thread pooling are implemented, and to lay the groundwork for designing powerful multithreaded servers.

Tomcat is the most widely used Java Web container , powerful and scalable. The latest version of Tomcat (5.5.17) uses the Apache portable Runtime (Apr) as the lowest level in order to improve responsiveness and efficiency, using a variety of techniques including sockets and buffer pools in the APR, and improves performance. Apr is also the lowest level of Apache httpd. It is conceivable that the same as the ASF (Apache Software Foundation) in the member, complementary interoperability is still a lot of, although the use of different development language.

The Tomcat thread pool is located in the Tomcat-util.jar file and contains two thread pool scenarios. Scenario One: Using APR's Pool technology, JNI is used; scenario two: ThreadPool with Java implementations. Here is the second kind. If you want to learn about APR's Pool technology, you can view the APR's source code.

ThreadPool creates 5 threads by default, saves them in a 200-D thread array, starts them when it is created, and, of course, handles the "Wait" state when there is no request (in fact, a while loop, waiting for notify). If a request is available, the idle thread is awakened to execute the user's request.

The specific request process is that when the service starts, create a one-dimensional array of threads (maxthread=200) and create an idle thread (minsparethreads=5) to wait for the user to request it at any time. When there is a user request, the Threadpool.runit (threadpoolrunnable) method is invoked to pass an instance that needs to be executed to the ThreadPool. Where the user needs to execute the instance must implement the Threadpoolrunnable interface. ThreadPool first finds an idle thread and, if so, runs it to perform threadpoolrunnable, and if there are no idle threads and no more than maxthreads, create minsparethreads idle threads at once If you have exceeded the maxthreads, wait for the idle thread. In short, to find an idle thread to use it to execute the instance. When found, removes the thread from the thread array. It then wakes up the idle thread that has been found and uses it to run the execution instance (threadpoolrunnable). After the threadpoolrunnable is run, the thread is put back into the thread array as an idle thread for subsequent use.

From this we can see that Tomcat's thread pool implementation is relatively simple, Threadpool.java also has only 840 lines of code. Save the idle thread with a one-dimensional array, creating an idle thread at a small pace (5) at a time and putting it into the thread pool . When used, remove the idle thread from the array, and then "return" it 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.