When I read Head First Servlet and JSP, they say that the instance variable is unsafe.
I do not understand this statement. For example: I have a servlet whose name ActionServlet.java. Each time each user request is sent to the server, the container creates a new stream and creates a new instance ActionServlet.
ActionServlet may have the structure:
public class ActionServlet extends HttpServlet {
Instance variable;
public void processRequest(HttpServletRequest request, HttpServletResponse response) {
}
}
So, since all of these threads create a new instance of the class for ActionServlet, so I don't see any problem here. because the instances of these threads are separate from each other.
Please find out where the problem is when using an instance variable in a multi-threaded environment.
Thank:)
source