1

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.

Bagesh Sharma
  • 783
  • 2
  • 9
  • 23
  • @BalusC my question is related to Stack Memory ... How servlets method will get loaded in stack memory. Please clear my confusion. – Bagesh Sharma Jan 17 '20 at 09:27
  • 1
    It's not different from normal Java classes. Servlet classes are absolutely not treated differently than normal Java classes. – BalusC Jan 17 '20 at 11:29
  • @BalusC Ok got it ... I was confused about stack memory in multithreading in Servlets ... Do you have any good reference documents related to stack memory in multithreading? Thanks for your support. – Bagesh Sharma Jan 20 '20 at 05:47

0 Answers0