码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet 异步处理

时间:2019-11-26 13:17:57      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:col   extends   tst   ide   ons   png   线程   throws   主线程   

@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 异步处理

标签:col   extends   tst   ide   ons   png   线程   throws   主线程   

原文地址:https://www.cnblogs.com/qin1993/p/11934732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!