Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Is the following servlet thread safe?

 
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
 
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).]
 
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
 
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.
 
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.
 
Would you like to try a free sample? Today we are featuring tiny ads:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic