• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

SingleThreadModel Servlet with class variable

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a Servlet implments SingleThreadModel then the local, instance and request variables are thread safe.
Therefore, I think I could said the belowing serlvet, which implements SingleThreadModel and has a instance variable, is thread safe, right?

----------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet implements SingleThreadModel {
public String var; //Instance variable
public doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServeltException{
.....
}
}
----------------------------------------------

But can we say the Servlet is ThreadSafe if it implements SingleThreadModel but has a "class variable"??
Like this:
----------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet implements SingleThreadModel {
static public String var; //class variable
public doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServeltException{
.....
}
}
----------------------------------------------
Is this Servlet is ThreadSafe? Cause I think class variable is never thread-safe.
Thank you.
 
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You right, class variable is not thread safe although we implement SingleThreadModel.
Web container may created some instances of that servlet but class variable is created only one fot those instances.
Correct me if I am wrong
daniel
 
Author
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, Daniel.
I do not think, that the term "Threadsafe servlet" is a good choice here.
We do not not know, what the example servlets do in their service-methods. Maybe, they access the servlet context without synchronization, ...
Greetings from Hamburg,
Stefan
 
Don't MAKE me come back there with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic