• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

single-threaded

 
tony kanvas
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet can be made thread safe by running it in the single-threaded model.
isnt this statment true ??
 
Mike J. Beaty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't the SingleThreadModel mean that the servlet container will never create more than one thread for this servlet?
 
John Pickler
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic