When I read Head First Servlet and JSP
, they say that instance variable is non-thread safe.
I don't understand this statement so much. For example: I have a servlet which name is ActionServlet.java
. Each time, each user's request is sent to server, container will create a new thread and create new ActionServlet
instance.
ActionServlet
may be has a structure:
public class ActionServlet extends HttpServlet {
// example of instance variable
Instance variable;
public void processRequest(HttpServletRequest request, HttpServletResponse response) {
// process something relating to instance variable
}
}
So, because all these threads create a new class instance for ActionServlet
, so I don't see any problem here. because instances of these thread is separate of each other.
Please figure out where problem when use instance variable in multithread environment.
Thanks :)