Java异步servlet仍在阻止应用程序

时间:2018-11-03 12:48:47

标签: java tomcat servlets java-ee

这是我的代码:

@WebServlet(value = "/path", asyncSupported = true)
public class Path extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public Path() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);

    AsyncContext ac = request.startAsync();
    Runnable r = new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {


                ac.getResponse().getWriter().append("Served at: ").append(request.getContextPath());
                ac.complete();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    new Thread(r).start();



}

当我连续两次运行此命令时,第二次等待第一个完成,然后再开始运行,这要花10秒以上的时间,而不是运行异步并在第一个之后立即完成。我该如何解决?

0 个答案:

没有答案