• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SingleThreadModel

 
senthil rajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
senthil rajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rex & santosh,

Its pretty clear now.


Regards,
Senthil.
 
singh santosh
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alek its clear now

regards
-santosh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic