Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why create new thread with startAsync instead of doing work in servlet thread?

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?

like image 898
Aivar Avatar asked Apr 08 '12 20:04

Aivar


2 Answers

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 comet. 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.

like image 73
Tomasz Nurkiewicz Avatar answered Sep 27 '22 22:09

Tomasz Nurkiewicz


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

like image 26
shashank Avatar answered Sep 28 '22 00:09

shashank