What's the purpose of AsyncContext.start(…) in Servlet 3.0?

后端 未结 4 2090
[愿得一人]
[愿得一人] 2020-12-12 23:29

Servlet API says about \"AsyncContext.start\":

void start(java.lang.Runnable run)

Causes the container to dispatch a thread, possibly from

4条回答
  •  别那么骄傲
    2020-12-13 00:21

    I had the same reaction at first -- if you're just passing the work to another thread, what do you gain? The spec is not much help in explaining why this is a good idea. But this post does an excellent job. Basically, it's to let the server degrade gracefully under heavy load rather than just fail by running out of threads. The actual work is done in a fixed-size pool of threads, so the server can accept any number of requests without having to keep a thread around for each one until it completes. Of course, you may have to tweak your O/S settings to be able to keep open thousands of sockets at a time.

    Once you have this ability, you can more easily take advantage of the Comet (server push) architecture, where the client Javascript keeps an AJAX request open so that the server can notify it as soon as some event occurs, rather than having to poll the server to find out if something happened.

提交回复
热议问题