我正试图通过Tomcat 7 Http11NioProtocol从NIO中获益。
我刚读了一篇关于它的文章: Understanding the Tomcat NIO Connector
并查看:tomcat docs
我启动了只有SimpleServlet的简单服务器,它计算并发线程数,等待10秒并返回空结果:
public class SimpleServlet extends HttpServlet {
final static Logger logger = Logger.getLogger(SimpleServlet.class);
private static final AtomicInteger threadCntr = new AtomicInteger();
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
logger.info("ProxyService doGet threads[" + threadCntr.incrementAndGet() + "]");
try {
Thread.sleep(10000L);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
logger.info("end processing");
threadCntr.decrementAndGet();
}
}
}
配置tomcat的server.xml以使用nio协议:
<Connector port="8081" maxHttpHeaderSize="8192" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="200" minSpareThreads="5" maxSpareThreads="5"
enableLookups="false" redirectPort="8443" acceptCount="1"
connectionTimeout="20000" disableUploadTimeout="true" />
我开始发送轻量级请求并使用Java VisualVM监视它:
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 1000; i++) {
new Thread(() -> {
executeHttp(URL, "test"); // impl hided
}).start();
Thread.sleep(10);
}
}
当我运行测试时,我看到只使用了200个线程(这没关系) 但客户的请求不能超过每10秒200个
那么,BIO之间的区别在哪里? 或者我错过了一些配置? Http11NioProtocol的主要特点是什么?