Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are the SingleThreadModel servlets thread safe?

 
Nasko Aladjov
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
 
Anthony Watson
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.
 
Leandro Oliveira
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