Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is fundamental difference between NIO and BIO in Tomcat?

Tags:

tomcat

nio

With NIO connector set in Tomcat we have N pooler threads and M worker threads.

With BIO connector set we can have N*M threads in thread pool. So what will be the difference between two connectors ?

like image 765
voipp Avatar asked Apr 26 '19 16:04

voipp


People also ask

What is Nio in Tomcat?

The NIO connector (non-blocking I/O) is a bit more complicated. It uses the java NIO library and multiplexes between requests. It has two thread pools – one holds the the poller threads, which handle all incoming requests and push these requests to be handled by worker threads, held in another pool.

Does Tomcat use NIO?

NIO mode enables Tomcat to simultaneously handle multiple connections per thread by using poller threads to keep connections alive and worker threads to process incoming requests.

What is Tomcat connector?

Connector elements are Tomcat's links to the outside world, allowing Catalina to receive requests, pass them to the correct web application, and send back the results through the Connector as dynamically generated content.

Is Tomcat not blocking?

However, Tomcat also supports Non-Blocking I/O. It is said that it supports near 13,000 simultaneous requests in NIO mode. Still, there is a limit. The acceptCount attribute defines the maximum number of HTTP connections that will be put in a queue while there is no free connection available to serve.


1 Answers

In BIO each new connection is allocated a thread from the Connector thread pool and that thread stays assigned to that connection until the connection closes. This means threads are idle for long periods of time between requests (i.e. during HTTP keep-alive).

In NIO, each new connection is passed to the Poller. A Poller thread is notified when there is data on the connection to be processed. The Poller then allocates a thread to the connection from the Connector thread pool and that thread stays assigned to that connection until all the data has been read/written. The connection is then passed back to the Poller so the Poller can monitor for more data.

In short, this makes NIO more scaleable. BIO requires one thread in the thread pool for each connection. NIO can maintain many more connections than BIO and only requires one thread in the thread pool for each concurrently processed request.

like image 57
Mark Thomas Avatar answered Nov 11 '22 23:11

Mark Thomas