• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SingleThreadModel Servlet with class variable

 
Jo Liang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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.
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • 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
 
Stefan Zoerner
Author
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic