This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes SingleThreadModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "SingleThreadModel" Watch "SingleThreadModel" New topic
Author

SingleThreadModel

Veri
Greenhorn

Joined: Jul 01, 2002
Posts: 1
I have a Servlet that implements SingleThreadModel interface, and contain a instance variable (for example int count). The doGet(...) method increment and display this variable.The servlet name in the web.xml is: servlet1. For a SingleThreadModel servlet there is a pool of instances for each call with servlet1. Is the value of count variable consistent between servlet1 calls? Why?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Veri,
Please change your name to be compliant with JavaRanch's naming policy.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.
Thanks,
Cindy


"JavaRanch, where the deer and the Certified play" - David O'Meara
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Not necessarily. If the servlet engine spawns multiple instances of your servlet, each one will initialize its own instance variable, starting at zero.
To make it consistent, you need to make your variable static and use a synchronized get method.
If you only ever have one user, you might survive.
:roll:


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
 
jQuery in Action, 2nd edition
 
subject: SingleThreadModel
 
Similar Threads
Does SingleThreadModel ensure thread-safety?
SingleThreadModel
Servlet instances and its init method
problem of thread safe
HTTP 404 error problem