• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic