• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is the following servlet thread safe?

 
Pradeep Kanwar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
Is the following servlet which takes age from an html form as input ThreadSafe?
import javax.servlet.*;
import javax.servlet.http.*:
import java.io.*;
public class AgeServlet extends HttpServlet implements SingleThreadModel
{
int myAge;
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
response.setContentType("text/plain");
PrintWriter out = res.getWriter();
myAge = req.getParameter("age");
getAge(out);
out.close();
}
public void getAge(PrintWriter out)
{
out.println(myAge);
}
}
Thanks in advance
cheers
pradi
 
Kevin Yip
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be thread-safe since it implements SingleThreadModel interface and 'myAge' is not static class variable. By the way, I can't find the setAge() method. If this is the case, it's thread-safe even you haven't implement SingleThreadModel.
[This message has been edited by Kevin Yip (edited August 31, 2001).]
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet container may send concurrent requests through the service method of
the servlet. To handle the requests the developer of the servlet must make adequate
provisions for concurrent processing with multiple threads in the service method.
An alternative for the developer is to implement the SingleThreadModel inter-face
which requires the container to guarantee that there is only one request thread
at a time in the service method. A servlet container may satisfy this requirement
by serializing requests on a servlet, or by maintaining a pool of servlet instances.
The use of the SingleThreadModel interface guarantees that only one thread at a
time will execute in a given servlet instance�s service method. It is important to
note that this guarantee only applies to each servlet instance, since the container
may choose to pool such objects. Objects that are accessible to more than one serv-let
instance at a time, such as instances of HttpSession, may be available at any par-ticular
time to multiple servlets, including those that implement
SingleThreadModel.
Above two paragraphs directly come from the servlet specs. I also think the the given servlet is threadsafe. However if you are not implementing SingleThreadModel then it is a good idea to have all the variables local to the methods (as far as possible) and to synchronize the doGet, doPost etc.
Hope this helps
Chintan
 
Rishi Singh
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I think since the member variable here is not static and u r implementing the singlethreaded model,it's thread safe..the only scenario where it can go wrong is if the servlet container maintains a pool of instances...which means if there is a request thread for a servlet , the container can allocate any instance from the pool depending upon the availaibility...so in that case your concurrent request can be served by different instances from the pool . so if u r are implementing connection pooling then the above case may not exactly be called thread safe.
hope this clarifies.
 
deepak adlakha
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's thread safe and even as per servlet specficiation,it is thread safe,as it is implementing the SingleThreadSafe.
From specfication
"The use of the SingleThreadModel interface guarantees that only one thread at a time will execute in a given servlet instance�s service method. It is important to note that this guarantee only applies to each servlet instance, since the container may choose to pool such objects. "
SO it is clearly thread safe.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic