I have read from Servlet docs that only a single instance of the servlet is created inside the container to handle all requests and now I want to understand the internal working of request threads with functions of servlet like doGet or doPost.
As per my perception -
Case 1 -
Suppose two requests come to servlet instance for method doGet than this doGet method will be loaded in each thread own stack memory and the local variables of the doGet method will get space in that thread's own stack memory block.
So the Servlet method will be loaded in each thread's own stack memory and once the execution is complete that threads stack memory will be cleared and the response will be returned back to the client. There will be no concurrency issues in this because each thread's variables are in their own stack memory.
Case 2 -
If we defined any data structure like Arraylist in servlets instance variable and all requests want to update this Arraylist than the concurrency problems will come.
Please correct me if any of my perceptions (Case 1 or 2) are wrong or anybody can clarify this in a much better way.