Servlet 异步处理
Posted qin1993
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet 异步处理相关的知识,希望对你有一定的参考价值。
@WebServlet(value = "/asycservlet",asyncSupported = true) public class AsycServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("主线程启动"+Thread.currentThread()+"..."+System.currentTimeMillis()); AsyncContext startAsync=req.startAsync(); startAsync.start(new Runnable(){ @Override public void run() { System.out.println("异步程启动"+Thread.currentThread()+"..."+System.currentTimeMillis()); try { asyc(); AsyncContext asyncContext=req.getAsyncContext(); ServletResponse response=asyncContext.getResponse(); response.getWriter().write("asyc success"); } catch (IOException e) { e.printStackTrace(); }catch (InterruptedException e){ e.printStackTrace(); }finally { startAsync.complete(); } System.out.println("异步程结束"+Thread.currentThread()+"..."+System.currentTimeMillis()); } }); System.out.println("主线程结束"+Thread.currentThread()+"..."+System.currentTimeMillis()); } public void asyc() throws InterruptedException{ System.out.println(Thread.currentThread()+"......"); Thread.sleep(5000); } }
以上是关于Servlet 异步处理的主要内容,如果未能解决你的问题,请参考以下文章