public class ListServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("I am begin output !"); response.getWriter().flush(); //method one AsyncContext async = request.startAsync(); new AsyncOutput(async).start(); // method two new ThreadOutput(response).start(); response.getWriter().println("I am finash output !"); response.getWriter().flush(); } } class AsyncOutput extends Thread { private AsyncContext async; public AsyncOutput(AsyncContext async) { this.async = async; } public void run() { try { Thread.sleep(3000); async.getResponse().getWriter().println("I was three minutes late !"); async.getResponse().getWriter().flush(); }catch(Exception e) { e.printStackTrace (); } } } class ThreadOutput extends Thread { private HttpServletResponse response; public ThreadOutput(HttpServletResponse response) { this.response = response; } public void run() { try { Thread.sleep(3000); response.getWriter().println("I was three minutes late !"); response.getWriter().flush(); }catch(Exception e) { e.printStackTrace (); } } }
The two effects are the same That being the case, what else do you do with AsyncContext? What's different about AsyncContext What is the difference between the above two usages? ask for advice
Answer:
AsyncContext does not allow you to output asynchronously, but allows you to output synchronously, but liberates the use of threads on the server side. When using AsyncContext, for browsers, they are synchronously waiting for output, but for the server side, processing The thread of this request is not stuck waiting there, it is to transfer the current processing to thread pool processing, the key lies in the thread pool, the server side will start a thread pool to serve those requests that need to be processed asynchronously, and if you yourself If each request is processed by a thread, this may consume a large number of threads.
Your current use of AsyncContext is not best practice, it should actually be used like this:
final AsyncContext asyncContext = request.getAsyncContext(); //Add a listener to monitor the asynchronous execution result asyncContext.addListener(new AsyncListener() { @Override public void onComplete(AsyncEvent event) throws IOException { // handle the logic for normal termination here } @Override public void onTimeout(AsyncEvent event) throws IOException { // handle timeout logic here } @Override public void onError(AsyncEvent event) throws IOException { // handle error logic here } @Override public void onStartAsync(AsyncEvent event) throws IOException { // handle the logic of starting an asynchronous thread here } }); / / Set the timeout time, when the time is up, the onTimeout method will be called back asyncContext.setTimeout(10000L); //Start here, pass in a Runnable object, the server will put this Runnable object in the thread pool for execution asyncContext.start(new Runnable() { @Override public void run() { //Do time-consuming operations here, if done, call the complete method to notify the callback, and the asynchronous processing is over asyncContext.complete(); } });