最近我开始研究Java Servlet异步功能。我编写了如下所示的示例代码来检查aysnc功能。我在单核处理器上运行它。我从jsp提交了100个请求(图像请求)。我在代码中添加了“Request Submmited :::”SOP。它显示前0到6个请求的SOP,然后在一段时间后显示6到11的SOP ....为什么它不显示所有100个请求的SOP。
package com.test;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns={"/Test"}, asyncSupported = true)
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
public Test() {
super();
}
int counter = 0;
@Override
protected void doGet(final HttpServletRequest request,
final HttpServletResponse response) throws ServletException,
IOException {
System.out.println("Request Submmited:::" + counter++);
final AsyncContext ctx = request.startAsync();
ctx.start(new Runnable() {
public void run() {
try {
String count = request.getParameter("test");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileInputStream fin = new FileInputStream(
"D:/TESTImages/8_0_0_NUCLEI" + count + ".jpg");
byte[] data = new byte[fin.available()];
fin.read(data);
response.getWriter().print(new String(data));
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
ctx.complete();
}
});
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
对于在servlet容器中处理异步请求的池,似乎默认线程池大小为 5 。尝试提供init params,如下所示:
@javax.servlet.annotation.WebServlet(urlPatterns={"/Test"}, asyncSupported = true,
initParams = { @WebInitParam(name = "threadpoolsize", value = "100") })