"Internet Architecture" Software Architecture-Tomcat's thread source code, familiar with communication methods (on)

Today I talk about the tomcat thread source code model. If you want to use the tomcat thread source code model, the old iron can be familiar with the connection and high concurrency configuration. If you configure it before, you will benefit forever. Let’s talk about the threading model of tomcat.

Introduction to the four threading models supported by Tomcat

As of 8.0 tomcat supports four field models,

  • BIO

BIO was used before tomcat6.0, 8.0 is the default BIO, and the traditional java IO is actually socket.

  • NIO

Through the selector, it is assigned to the specified thread by request, just for reading. It will be released immediately after reading. Notify the client. Reading and writing are separated. The test is readable and writable. Each request creates a thread, and tomcat does not do thread control by itself. But through the thread pool. Currently, high concurrency mode is used, and NIO is used by default.

  • APR

Tomcat can install the APR library through JNI. Linux installs the linux version of the apache library, and windows installs the windows version of the apache library. Many people think that apache processes static files faster than tomcat. It may be before. The speed is basically the same now.

  • AIO

After tomcat7, because NIO has select, it is actually multiplex selector, AIO does not

Use the configuration method of the specified IO model:

Configure the modification in the server.xml file.
The default configuration 8.0 protocol="HTTP/1.1" Before 8.0 is BIO, after 8.0 is NIO

  • BIO (monogamous, only do one thing per request)
protocol=“org.apache.coyote.http11.Http11Protocol“
  • NIO (polygamy, one request is recycled for multiple tasks)
protocol=”org.apache.coyote.http11.Http11NioProtocol“
  • AIO (the middleman is no longer needed, one thread finishes reading and throws it to the next thread to handle writing)
protocol=”org.apache.coyote.http11.Http11Nio2Protocol“
  • APR
protocol=”org.apache.coyote.http11.Http11AprProtocol“

Tomcat BIO, NIO implementation source code analysis

You should have heard of blocking blocking BIO and non-blocking NIO before. How they did it. What is their principle?

Synchronization: Take the bank card to withdraw money by yourself (when using synchronous IO, Java handles IO reading and writing by itself);

Asynchronous: Entrust a younger brother to take the bank card to the bank to withdraw money, and then give it to you (when using asynchronous IO, Java delegates IO reading and writing to the OS for processing, and the data buffer address and size need to be passed to the OS (bank card and password), OS needs to support asynchronous IO operation API);

Blocking: ATM queues for withdrawals, you can only wait (when using blocking IO, the Java call will block until the read and write is completed before returning);

Non-blocking: Withdraw money at the counter, take a number, and then sit on a chair to do other things. The equal sign broadcast will notify you to go through it. You can’t go without it (when using non-blocking IO, if you cannot read or write, the Java call will return immediately. When the IO event dispatcher will notify you to read and write, then continue to read and write, and continue to loop until the read and write is complete)

Tomcat connector concurrency parameter interpretation

Connector usually uses the most, the maximum number of connections, the maximum timeout period, the connection package,

PS: NIO is to do the most things with the least number of threads, and BIO is to find more people to do it. They are all blocked, especially the selector.select() method, which is the same as bio's accept(), which is actually blocking. Comparing single-threaded and multi-threaded processing methods, in general, no matter which one, nio mode is better than bio.

Guess you like

Origin blog.csdn.net/zhugeaming2018/article/details/111850771