简体   繁体   English

Tomcat 中 NIO 和 BIO 的根本区别是什么?

[英]What is fundamental difference between NIO and BIO in Tomcat?

With NIO connector set in Tomcat we have N pooler threads and M worker threads.通过在 Tomcat 中设置 NIO 连接器,我们有 N 个池线程和 M 个工作线程。

With BIO connector set we can have N*M threads in thread pool.通过设置 BIO 连接器,我们可以在线程池中拥有 N*M 个线程。 So what will be the difference between two connectors ?那么两个连接器之间会有什么区别呢?

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.在 BIO 中,每个新连接都会从连接器线程池中分配一个线程,并且该线程一直分配给该连接,直到连接关闭。 This means threads are idle for long periods of time between requests (ie during HTTP keep-alive).这意味着线程在请求​​之间会长时间空闲(即在 HTTP 保持活动期间)。

In NIO, each new connection is passed to the Poller.在 NIO 中,每个新连接都会传递给 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.简而言之,这使 NIO 更具可扩展性。 BIO requires one thread in the thread pool for each connection.对于每个连接,BIO 需要线程池中的一个线程。 NIO can maintain many more connections than BIO and only requires one thread in the thread pool for each concurrently processed request. NIO 可以维护比 BIO 多得多的连接,并且每个并发处理的请求只需要线程池中的一个线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM