Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why instance variable in Servlet is not thread-safe [duplicate]

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 :)

like image 234
hqt Avatar asked Apr 21 '12 18:04

hqt


People also ask

Why instance variables are not thread-safe?

Such an object would not be thread-safe, because in a multithreaded environment, the object could become corrupted or be observed to have an invalid state. A thread-safe object is one that always maintains a valid state, as observed by other classes and objects, even in a multithreaded environment.

Is instance variable in servlet thread-safe?

By default, servlets are not thread-safe. The methods in a single servlet instance are usually executed numerous times simultaneously up to the available memory limit. Each execution occurs in a different thread, though only one servlet copy exists in the servlet engine.

Which variables are thread-safe in servlet?

Method variables are thread-safe, class variables are not. Not quite sure with Only instance variables need to synchronize . Static variables also need synchronization and they are not instance variables.


2 Answers

The mistake you are making is here:

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.

The container does not create a new instance of the Servlet class for each request. It reuses an existing one. This is why they are not thread safe.

The Stripes Action Framework DOES create a new instance for each request, so that's an okay assumption under that framework. However, for example, Struts 1 follows the Servlet model and does not create a new action per request.

That does not mean that the container is limited to a single instance, it in theory can create more than one, but it's not a specified behavior so can not be relied upon. Most of the popular ones do not.

like image 143
Will Hartung Avatar answered Oct 22 '22 21:10

Will Hartung


because all these threads create a new class instance (action.java), so I don't see any problem

You are supposing that every thread create a class instance that will be used only by that thread, that's why you don't have any problem.

But try to imagine, with your specific sample, that the same instance is accessed from two threads. What's happend if both use in the same time your request and response members? Maybe you will read data from unidentifiable request, and you will write an inconsistent response that mix two parts.

So in you case too, instance variable are not thread safe, because if two thread access the same instance they can disturb each other.

like image 34
dash1e Avatar answered Oct 22 '22 20:10

dash1e