Originally posted by Bear Bibeault: Since there is only one copy of the servlet created, the only thread-safe variable are automatic variables inside the methods.
Or instance scoped final variables. IOW: variables declared final but not initialized in the declaration. Once initialized (usually in the servlet's init method) they are frozen like constants. Since they can't be changed, they are thread-safe.
Originally posted by Bear Bibeault: Since there is only one copy of the servlet created, the only thread-safe variable are automatic variables inside the methods.
What led you to believe that turning instance variables into class variables would somehow make something thread-safe?
I have this old "Core Servlets" book by Marty Hall. There is a passage regarding the isThreadSafe attribute where he says:
Be careful about using isThreadSafe="false" when your servlet has instance variables (fields) that maintain persistent data. In particular, note that servlet engines are permitted (but not required) to create multiple servlet instances in such a case and thus instance variables are not necessarily unique. You could still use static fields in such a case, however.
I guess he means the static fields would be thread safe only if you implement the SingleThreadModel. Right?
Drew
Nikhil Menon
Ranch Hand
Joined: Nov 22, 2004
Posts: 70
posted
0
Hi,
Static variables are the least thread safe as other.
Static variables are never thread safe. These variables are at class level, so they are shared between all instances. Hence these variables are not thread safe even if the servlet is implementing the SingleThreadModel interface. That is why they are usually used to store only constant/read-only data.
I guess he means the static fields would be thread safe only if you implement the SingleThreadModel. Right?
Drew
Static variables would be un-safe even if you were implementing SingleThreadedModel.
A static variable is one shared by all instances of the class.
Jack Daniel
Ranch Hand
Joined: Jun 15, 2002
Posts: 163
posted
0
I very well know, that the Static members are not thread safe,.. but in my servlets I always use:
as a class member,
because i do the data source JNDI lookup in init() and get connection in other methods...
This is the only static member I have; and definitely NO instance members. I know this is not thread safe.. but there is no need for thread safety on this issue.. Your opinion on this?? is it a bad approach?? thanks
Drew Lane
Ranch Hand
Joined: May 13, 2001
Posts: 296
posted
0
OK, I tried to get rid of all the instance variables to be safe.
However, I still have some final static instance variables that are initialzed in the class. I don't understand why these might cause a thread safety issue, too? If they are final, why aren't they "set in stone"?
One could even go as far to say that non-final variables that are set up once during init() and never touched again (read-only from that point on) would be safe -- but that's a slippery slope...
Originally posted by Bear Bibeault: Actually you cannot make them final if they are set in init()
Ack!!!, you're right.
I use servlet init params quite a bit and had plans to solidify my servlets by making them final, with deferred assignment taking place in the init method.
In a nutshell, the compiler has to know that the assignment comes before any other access. Since the compiler doesn't know that the init method will always be called before one of the service methods, it blows up. Oh well, Thanks Bear.. [ February 17, 2005: Message edited by: Ben Souther ]
Raja gopal Y
Greenhorn
Joined: Mar 08, 2005
Posts: 15
posted
0
What i am thinking is, All the variables inside the service()[or doGet / doPost] method are not Threadsafe, all the remaining variables are ThreadSafe.
Because Only the Service method is called many times by different threads on a servlet instance.
Originally posted by Raja gopal Y: What i am thinking is, All the variables inside the service()[or doGet / doPost] method are not Threadsafe, all the remaining variables are ThreadSafe.
Because Only the Service method is called many times by different threads on a servlet instance.
Any Comments !!!
No, you've got it backwards. The variables inside the service methods are thread safe because they are accessible only to the thread that owns them. Instance variables are NOT threadsafe because they are accessible to all threads from within that servlet.