Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Does declaring class variable as private force servlets to be thread-safe?

Reply
Thread Tools

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

 
 
Marc Twain
Guest
Posts: n/a
 
      11-13-2003
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>");

}

}
 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      11-13-2003
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
(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Marc Twain
Guest
Posts: n/a
 
      11-14-2003
> (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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nike air force one, air force 1, air force one low cut, air force one abdul_razak@indiatimes.com Digital Photography 2 12-31-2008 04:29 PM
Nike Air Force Ones,Air Force One Air Force One-1 lky52193@gmail.com Computer Support 0 01-17-2008 04:40 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:46 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:34 PM
Declaring a private field in class Johs C++ 4 05-02-2007 09:36 AM



Advertisments