Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Difference between SingleThreadModel and synchronized..

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why cannot I just make my doXXX methods synchronized instead of having my servlet implement SingleThreadModel?
Or Can I?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The issue here is performance. If you go ahead with the synchronization approach then it'll probably create a bottle-neck there because no matter what only one thread can run the doGet() at any instant of time.
Generally speaking, if the doGet() have a lot of things to do, it's good design to synchronize only those code blocks within the doGet() that cause non-deterministic behavior so as to mitigate the performance problem as much as poosible.
If your server can take high loading, it may be better, by using the SingleThreadModel approach, to create a pool of servlet instances so as to allow processing requests in parallel.
 
Enthuware Software Support
Posts: 4599
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can but it will be a performance hog.
If you implement SingleThreadModel, the container runs only one thread in the service method, but to service multiple requests parallelly, the container is free to instantiate multiple instances of your servlet class.
If, instead of implementing SingleThreadModel interface, you synchronize your doXXX methods, you will get the worst of both worlds. Neither the requests will be served parallelly (because of synchronized) and nor the container will be free to instatiate multiple objects (because of not implementing STM). Of course, in some cases, this is what you might want!
HTH,
Paul.
------------------
SCJP2, SCWCD Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
JQPlus - For SCJP2
JWebPlus - For SCWCD
JDevPlus - For SCJD
 
Jim Bertorelli
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, Kevin and Paul.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic