• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SingleThreadModel

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alek its clear now

regards
-santosh
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic