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

single-threaded

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A servlet can be made thread safe by running it in the single-threaded model.
isnt this statment true ??
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be exact you implement the SingleThreadModel. This is not always thread safe. If you have declared static class variables only one copy of these will be available for all of the threads. Also, any use of session or context attributes will not be thread safe either. You could synchronize the block of code that access these but you would be taking a huge performace hit if there were any significant amount of users.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't the SingleThreadModel mean that the servlet container will never create more than one thread for this servlet?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct, but as one exam software I use pointed out, that doesn't prevent your "single threaded" Servlet from launching its own threads. Those threads could then make your single Servlet instance not "thread safe". The bottom line for the exam is that implementing the SingleThreadedModel interface definitely does not guarantee your Servlet to be thread-safe.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that it guarantees one thread per servlet instance but containers can pool instances of the servlet class.
 
tony kanvas
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it will depends attribute scopes that used , like if we use local var it will be thread safe
Instance var will not ,and class var (static) will not too .
I think the question a above missing to clarify the attribute scopes type.
am i right?
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the question probably, and I can only say probably, is to make you realize that implementing SingleThreadModel doesn't in and of itself guarantee thread safety. There are a number of gotchas that need to be kept in mind when using it. The Deshmukh book says one reason not to use it is:
"a false sense of thread safety."
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic