zoukankan      html  css  js  c++  java
  • 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);
        }
    }

     

  • 相关阅读:
    POJ 3468 A Simple Problem with Integers
    BZOJ 4430 Guessing Camels
    POJ 2309 BST
    POJ 1990 MooFest
    cf 822B Crossword solving
    cf B. Black Square
    cf 828 A. Restaurant Tables
    Codefroces 822C Hacker, pack your bags!
    [HDU 2255] 奔小康赚大钱
    [BZOJ 1735] Muddy Fields
  • 原文地址:https://www.cnblogs.com/qin1993/p/11934732.html
Copyright © 2011-2022 走看看