• 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:

is this thread safe?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please tell me whether this servlet would be thread-safe?

import javax.servlet.*;
import javax.servlet.http.*:
import java.io.*;
public class TestServ extends HttpServlet implements SingleThreadModel
{
String index;
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
response.setContentType("text/plain");
PrintWriter out = res.getWriter();
myName = req.getParameter("value");
doFunction(out);
out.close();
}
public void doFunction(PrintWriter out)
{
out.println(index);
}
}
-----------
I think it would be because although a class variable index is used, it is called and initialized in the service method.
However, if there was another function in the class, and the index variable is maipulated, the servlet would no longer be thread safe.
is my logic correct?
please reply
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
By the spec, if the Servlet implements the SingleThreadModel interface, for every access of the servlet a seperate instance is being created and this instance would service the client.
Ideally the answer for your question is yes.But a word of caution is that it actually depends on the implementation of the server.

NOTE: You shouldn't try to overload the service() method if the class extends HttpServlet.
thanks,
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to my understanding this servlet is thread safe because it implements SingleThreadModel. This comes from the specification.
"For a servlet implementing the SingleThreadModel interface, the servlet container may instantiate multiple instances to handle a heavy request load and serialize requests to a particular instance."
******
What I get out of this is, if a servlet implements SingleThreadModel interface, the servlet container must either create a new instance for every other request or if the container wants single instance to handle multiple requests, it must maintain a queue of incoming requests and handover the request to the instance only after it has finished servicing previous request.
******
I am not 100% sure about what I said between ****** and ****** but I strongly believe that the given example is threadsafe.
Chintan
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javax.servlet.SingleThreadModel is a tag interface that ensures a servlet is thread-safe or the servlet�s service(), doGet(), doPost() methods can�t be executed by more than one thread at a time. So the above index String is thread-safe as that will be accessed thru service() which becomes thread-safe due to implementation of ThreadSafe interface....
But if another object is able somehow to call doFunction() which is not automatically synchronized even after implementing SingleThreadModel interface then this class is not fully thread-safe. But we know there is no way to skip the init(),service(),destroy() life cycle's service() which must be used if u wanna call a servlet object where we are initializing index String. So happily safe....
------------------
Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer for the Java� 2 Platform
--When you learn something, learn it by heart!

[This message has been edited by Ashik uzzaman (edited December 30, 2001).]
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to Servlet API documentation SingleThreadModel Servlet must guarantee "that no two threads will execute concurrently in the servlet's service method." To accomplish this, each thread uses a free servlet instance from the pool. Thus, any servlet implementing SingleThreadModel can be considered thread safe & isn't required to synchronize access to its instance variables.
-Jyothi
 
Ashik Uzzaman
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yah, Jhyoti that's what i understand abt it...

------------------
Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer for the Java� 2 Platform
--When you learn something, learn it by heart!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys, i know this is too late to post any question related to this topoic.
Ofcourse SingleThreadModel guarantees thread safety for above example.
What if i have a static veriable in the servlet class ? Is that variable still thread safe under SingleThreadedMedel ?? will there be any issue for this variable value ??
Thanks
Ajay
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No the static variable will not be thread safe as it belongs to the class and can be changed by any member of the class. In this case that would be any instantiated servlet object.
To make it thread safe you can of course declare it synchronized. Which would then allow only one thread to "own" the object at any time.
Hope this helps.
Tom
 
expectation is the root of all heartache - shakespeare. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic