7

In servlet 3.0 one can use startAsync to put long work in another thread, so that you can free up servlet thread.

Seems that I'm missing something, because I don't see, why not just to use servlet thread for working? Is the thread created by startAsync somehow cheaper?

Aivar
  • 6,814
  • 5
  • 46
  • 78

2 Answers2

17

In most situations when handling requests you are blocking or waiting on some external resource/condition. In this case you are occupying the thread (hence a lot of memory) without doing any work.

With servlet 3.0 you can serve thousands of concurrent connections, much more than available threads. Think about an application that provides downloading of files with limited throughput. Most of the time your threads are idle because they are waiting to send next chunk of data. In ordinary servlets you cannot serve more clients than the number of your HTTP threads, even though most of the time these threads are idle/sleeping.

In servlet 3.0 you can have thousands of connected clients with few HTTP threads. You can find a real world example in my article: Tenfold increase in server throughput with Servlet 3.0 asynchronous processing inspired by this question: Restrict download file bandwidth/speed in Servlet

Is the thread created by startAsync somehow cheaper?

There is no thread created by startAsync! It just tells the servlet container: hey, although the doGet/doPost method finished, I am not done with this request, please do not close. That's the whole point - you probably won't create new thread per each async request. Here is another example - you have thousands of browsers waiting for a stock price change using . In standard servlets this would mean: thousands of idle threads waiting for some event.

With servlet 3.0 you can just keep all asynchronous requests waiting in an ArrayList or a some queue. When the stock price change arrives, send it to all clients one after another. Not more than one thread is needed in this scenario - and all HTTP threads are free to process remaining resources.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • ... but unfortunately it's not totally clear for me, yet. Looks like I got confused because of the example I learned AsyncContext from. I wrote another question concerning this, maybe you could also look at this: http://stackoverflow.com/questions/10073392/whats-the-purpose-of-asynccontext-start-in-servlet-3-0 – Aivar Apr 09 '12 at 12:48
4

With servlet 3.0 you can just keep all asynchronous requests waiting in an ArrayList or a some queue Problem is this. You still need a new thread to process the request and pick up the request to finally send the response. So we free up http threads but have to create some thread to process the request

shashank
  • 41
  • 1
  • 2