huahua
huahua

Reputation: 285

how asynchronous servlet processing improve performance

I read from http://docs.oracle.com/javaee/7/tutorial/doc/servlets012.htm

Java EE provides asynchronous processing support for servlets and filters. If a servlet or a filter reaches a potentially blocking operation when processing a request, it can assign the operation to an asynchronous execution context and return the thread associated with the request immediately to the container without generating a response. The blocking operation completes in the asynchronous execution context in a different thread, which can generate a response or dispatch the request to another servlet.

I am wondering where is the

different thread

come from? Assuming the container has 10 threads, 5 of them are processing request, we have to use another 5 to process the long running business logic?. where do we get the performance improvement? the total threads available to use is limited, right?

Thanks.

Upvotes: 1

Views: 923

Answers (1)

Braj
Braj

Reputation: 46841

Read more about Servlet 3 0 final-spec - Section 2.3.3.3 - Asynchronous processing where it is explained in detail.

It causes the container to dispatch a thread, possibly from a managed thread pool, to run the specified Runnable. AsyncContext is a standard way defined in Servlet 3.0 specification to handle HTTP requests asynchronously.

Basically HTTP request is no longer tied to an HTTP thread, allowing us to handle it later, possibly using fewer threads. It turned out that the specification provides an API to handle asynchronous threads in a different thread pool out of the box.


Read more about Executors.newFixedThreadPool() that creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available.

Please have a look at ExecutorService to read more about it along with sample code.

Upvotes: 2

Related Questions