• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Difference between SingleThreadModel and synchronized..

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4338
35
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks, Kevin and Paul.
 
Tell me how it all turns out. Here is a tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!