Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Are the SingleThreadModel servlets thread safe?  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I am sure i am posting a question which might be obvious to many, but it is confusing to me :
Are the servlets implementing SingleThreadModel inteface thread safe ?
i found several books and resources which state:
1. Whizlabs SCWCD simulator:

The simplest way to ensure that a servlet is thread safe is to implement the SimpleThreadModel inteface./QUOTE]
2. SCWCD Exam Study Kit:

A false sense of thread safety. Many novice developers think that implementing the SingleThreadModel interface allows them to forget about thread safety altogether.However,implementing this interface does not alleviate the problems associated with sharing the data through common means such as HttpSession and ServletContext because the requests may still be processed simultaneously by multiple instances.


3. The Servlet spec:

...This interface does not prevent synchronization problems that result from servlets accessing shared resources such as static class variables or classes outside the scope of the servlet.


-------------
To conclude:
I know that it depends on the context of the example test question, but if i have a generic question on the exam, regarding the thread safety of a servlet implementing the SingleThreadModel interface, what should be my answer?
King regards,
~Nasko

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should say that implementing the SingleThreadModel does not guarantee that your code is thread safe. For example, your code would not be thread safe if it wrote out to a particular file in an unsynchronized fashion. The container could create 10 instances of your servlet and all 10 could be trying to write to the same file at the same time. This demonstrates how the SingleThreadModel does not guarantee thread safety.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
however, if your code uses only instance variables, implementing single thread model makes your servlet thread safe!! When implementing single thread model the container allows only one thread in your service method per time! And creates other instances of this servlet if necessary! That's why usage of static variables (if not synchronized correctly) isn't thread safe, even if you implement SingleThreadModel!!
 
Nasko Aladjov
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony and Leandro,
Thank you for your response !
I would like to clarify that i have no problems understanding the behaviour of the SingleThreadModel interface and the influence of the variables .
My concern is how to answer that question on the exam ...
Any ideas ?
Kind regards,
~Nasko
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!