This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes SingleThreadModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "SingleThreadModel" Watch "SingleThreadModel" New topic
Author

SingleThreadModel

senthil rajan
Ranch Hand

Joined: Aug 31, 2005
Posts: 36
Hi Frnds,

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??

Thanks,
Senthil.
singh santosh
Ranch Hand

Joined: Jan 13, 2006
Posts: 136
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.

regards
-santosh
Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
Hi Senthil,

- 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)
senthil rajan
Ranch Hand

Joined: Aug 31, 2005
Posts: 36
Thanks Rex & santosh,

Its pretty clear now.


Regards,
Senthil.
singh santosh
Ranch Hand

Joined: Jan 13, 2006
Posts: 136
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??

thanks
-santosh
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
You can have multiple servlet instances in the pool, so you can have a situation when two servlets will be modifying the same static variable.
singh santosh
Ranch Hand

Joined: Jan 13, 2006
Posts: 136
Thanks Alek its clear now

regards
-santosh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SingleThreadModel
 
Similar Threads
Doubt in Thread Safe Servlet.
Regarding SingleThreadModel from J@Whiz
session attributes thread safe?
Which Attribute Scopes Are Thread-Safe?
problem of thread safe