When I implement SingleThreadModel interface to the servlet, which are the following variables are thread-safe? - class variable - static variable - instance variable - context variable - sesson variable - request variable (Its always thread-safe) - local variable (Its always thread-safe)
I'm bit confused abt it.Can anybody help me on this??
hi , According to me except for session and context variable all are thread safe .
Explanation: Since we are implementing SingleThreadModel ,there will never be more than one thread for one instance,so class ,instance and static variable are not at risk , yeh , since context and session varibales have application scope they are not thread safe as they can be accessed by many threads(of different instances 1 thread for 1 instance ) of same application.
- request, response, final and local variables are ALWAYS thread safe. - context and session are not thread safe even if we implement SingleThreadModel interface that's why we need to synchronize them if we need it to become thread safe. - class or static variables are not thread safe even if SingleThreadModel is implemented. - instance variables are by default not thread safe but if SingleThreadModel is implemented it becomes thread safe.
Hope this helps.
"If you fail to plan, you plan to fail."<br /> <br />Chad<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS (next)
Joined: Aug 31, 2005
Thanks Rex & santosh,
Its pretty clear now.
Joined: Jan 13, 2006
hi rex, Please clarify this
class or static variables are not thread safe even if SingleThreadModel is implemented.
According to me since we are implementing SingleThreadModel, class and static variable can not be accessed by more than one thread then how they are not thhread safe??