but if it is a static private instance variable then its not thread-safe even though it implements SingleThreadModel.
Rajesh <br /> <br />SCJP1.4 SCWCD1.4 SCBCD 1.3 ,SCDJWS(Preparing..)<br /> <br />There is no free will.It is the phenomenon bound by cause and effect.But there is something behind will which is free---Swami Vivekananda...
Private is an access modifier. Which means variables declared with private modifier CAN be accessed by any method(non-static) within the same class.
When 10 users are requesting the same resource 10 threads will be accessing the doGET() method and if you are accessing the private instance variable inside the doGET() method there is a possibility of one thread accessing the value set by another thread which could lead to data corruption.
Thats why it's instance variables are ALWAYS NOT thread safe.
But did u notice that the class in the given example implements SingleThreadModel(even though it is deprecated)..which means no multiple threads for a single instance..instead multiple instances will be created based on the request.
Then how do u conclude instance variables in the above class are not thread safe?.Plz explain..
The servlet in question implements SingleThreadModel and can be therefore considered thread-safe. The variable "strName" is thread-safe. There is no need to synchronize access to instance variables in Servlets implementing SingleThreadModel.
Having said that here is something one must keep in mind (excerpted from the Servlet spcn):
...SingleThreadModel does not solve all thread safety issues. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used.
It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. This interface is deprecated in Servlet API version 2.4.