So I read this great thread: Difference between each instance of servlet and each thread of servlet in servlets?
And it says "the servletcontainer reuses the same servlet instance for every request. "
So let's say we have a servlet:
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object thisIsThreadSafe;
thisIsThreadSafe = request.getParameter("foo"); // OK, this is thread safe.
}
}
So does the servlet container, when initializing it will call internally:
MyServlet myServlet = new MyServlet(....);
And then when a request matches what is in the web.xml, it will return this instance myServlet.
What I need help understand is, when the call to:
myServlet.doGet(..)
How does multi-threading work when there is only 1 instance? This isn't specific to servlets really, I just can't wrap my head around how this works.
Say there are 10 concurrent users on the website hitting this same servlet at the exact same time, this instance is shared accross all of them, why doesn't it then block and work in a serial manner, how does it do this concurrently?