When we built a back-end server using Java in the past, threads have been essential. By the way, technically, because making and destroying the thread have to cost a lot of resources, whenever some requests or events happen, making and destoroying the thread is very inefficient. Because of this, people came up with the concept of the pool which has many threads created initially and controls the life-cycle of the threads, and the servlet has been based on the pool.(As the servlet is a kind of the thread to connect in http, this has also the pool which is Servlet Pool). Even though the concept of the pool had improved the back-end server on higher performance, the other issue happened. Basically, threads consume many resoureces when threads are made and destroied, and if the thread pool consumes all of the threads, next requests would stand by in a queue, and if the queue is also full with the request, finally, the server would occur an error which is ‘Out Of Memory’. So, for this solution, even if we can come up with creating much more threads, it doesn’t become a good solution because creating much more threads increases usage of the memory (because the thread has their the value of a stracktrace and their own data).
So, as many people had wanted to improve the performance, the new solution was proposed by the servlet 3.0. This solution is to divide the thread into the pool of the servlet thread and the worker thread, and in the servlet 3.1 callback function was adopted. This solutions have many benefits. At first, according to separting the thread, Threads can be separated by role. Before servlet3.0, if some work takes a long time in the business logic, the servlet thread couldn’t be returned until some work end. However, as the threads are divided by role, the servlet has been able to be returned without some work end in the business logic. It makes the server more efficient.
- num1,2: This is the servlet thread of processing the request from client. If the servlet thread end processing the request, the servlet is returned to the pool of the servlet.
- num3,4: In order to processing the business logic, the worker thread is called from the pool of the worker thread. durig this process, since the servlet which was called for processing the request was already returned, this servlet is readying to deal with the new request.
- num5,6,7: After finishing the business logic, in order to respond the result to client, the servlet is called again from the pool of the servlet thread.
However, at this point, we are able to have a question. Finally, the pool of the worker thead would consume all of the thread, and the queue is also full with the request, and in the end, the server would occur the error which is ‘Out Of Memory’. Yes, it’s true, yet we can solve this issue using DeferredResult(I am going to talk about this one next post.). Likewise, in this article, what I want to say is that the threads have been separated into the pool by role. In the next article, I am going to talk more deeply using Code.