Does declaring class variable as private force servlets to be thread-safe?

Discussion in 'Java' started by Marc Twain, Nov 13, 2003.

  1. Marc Twain

    Marc Twain Guest

    This is driving me mad - got it from a New Rider's book:
    They are trying to demonstrate the use of 'implement
    SingleThreadModel'.

    You run the code below in 2 browser windows. Without SingleThreadModel
    of course the 2 numbers are read in parallel and the result are
    incorrect. With SingleThread Model you can see one browser waiting for
    the first thread to complete before going further. I understand that.

    BUT if you do not implement SingleThreadModel and declare counter as
    private, you get the same results as a Thread Safe servlet. So... what
    gives? Is Tomcat 'smart' enough to guess it should make the servlet
    thread safe to avoid erroneous results? The book doesn't mention this
    at all...

    TIA,

    AJ.


    -------------

    import javax.servlet.*;
    import java.io.*;

    public class ThreadSafeServlet extends GenericServlet {

    int counter = 0; // Try to make this private later on

    public void service(ServletRequest request, ServletResponse response)
    throws ServletException, IOException {

    // read
    BufferedReader in = new BufferedReader(new
    FileReader("C:\\jakarta-tomcat-4.1.29\\webapps\\myapp\\WEB-INF\\counter.txt"));
    counter = Integer.parseInt(in.readLine());
    in.close();

    System.out.println("counter prior: " + counter);

    // increment
    counter++;

    try {
    Thread thread = new Thread();
    thread.sleep(2000);
    }
    catch (InterruptedException e) {
    }


    // save
    BufferedWriter out = new BufferedWriter(new
    FileWriter("C:\\jakarta-tomcat-4.1.29\\webapps\\myapp\\WEB-INF\\counter.txt"));
    out.write(Integer.toString(counter));
    out.close();

    // display
    PrintWriter httpOut = response.getWriter();
    httpOut.println("<HTML>");
    httpOut.println("<HEAD>");
    httpOut.println("<TITLE>");
    httpOut.println("ThreadSafeServlet");
    httpOut.println("</TITLE>");
    httpOut.println("</HEAD>");
    httpOut.println("<BODY>");
    httpOut.println("<B>" + counter + "</B>");
    httpOut.println("<BR>");
    httpOut.println("</BODY>");
    httpOut.println("</HTML>");

    }

    }
     
    Marc Twain, Nov 13, 2003
    #1
    1. Advertising

  2. Re: Does declaring class variable as private force servlets to bethread-safe?

    Marc Twain wrote:

    > This is driving me mad - got it from a New Rider's book:
    > They are trying to demonstrate the use of 'implement
    > SingleThreadModel'.
    >
    > You run the code below in 2 browser windows. Without SingleThreadModel
    > of course the 2 numbers are read in parallel and the result are
    > incorrect. With SingleThread Model you can see one browser waiting for
    > the first thread to complete before going further. I understand that.
    >
    > BUT if you do not implement SingleThreadModel and declare counter as
    > private, you get the same results as a Thread Safe servlet. So... what
    > gives? Is Tomcat 'smart' enough to guess it should make the servlet
    > thread safe to avoid erroneous results? The book doesn't mention this
    > at all...


    [code elided]

    No. No application server I know, including Tomcat, does an analysis
    sufficiently deep to determine that a servlet not implementing
    SingleThreadedModel is nevertheless not thread-safe. Very likely the
    cause of the behavior you saw was not related to the access specifier of
    the servlet. I can think of two good alternative explanations right off
    the top of my head:

    (1) Tomcat wasn't using the version of the servlet class you thought it
    were using (i.e. for one reason or another it was still using the one
    that implemented SingleThreadedModel)

    (2) In addition to making the variable in question private, you also
    made it static. That would not make the servlet thread-safe (far from
    it!), but it might produce behavior that looked similar in this case.

    There are other possibilities, but I think those are the two most likely.


    John Bollinger
     
    John C. Bollinger, Nov 13, 2003
    #2
    1. Advertising

  3. Marc Twain

    Marc Twain Guest

    > (1) Tomcat wasn't using the version of the servlet class you thought it
    > were using (i.e. for one reason or another it was still using the one
    > that implemented SingleThreadedModel)
    >
    > (2) In addition to making the variable in question private, you also
    > made it static. That would not make the servlet thread-safe (far from
    > it!), but it might produce behavior that looked similar in this case.



    John- Thank you for your reply -

    Well I have deleted the class files, rebuilt them, rebooted the
    machine, etc. and I still get the same behaviour as long as the class
    var is marked private. I also made sure I didn't make it static. So
    neither 1 or 2 apply here.

    I am going to continue investigating the issue and will post and
    findings in this thread - in the meantime if a guru out there knows
    what's going on, feel free to pitch in :)

    TIA,

    AJ.
     
    Marc Twain, Nov 14, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaveLessnau
    Replies:
    3
    Views:
    429
    Howard
    May 16, 2005
  2. Johs
    Replies:
    4
    Views:
    312
    Jim Langston
    May 2, 2007
  3. Gabriel Rossetti
    Replies:
    0
    Views:
    1,332
    Gabriel Rossetti
    Aug 29, 2008
  4. Rie!
    Replies:
    3
    Views:
    133
  5. John Nagle
    Replies:
    5
    Views:
    475
    John Nagle
    Mar 12, 2012
Loading...

Share This Page