Sandeep Prakash wrote:
>
> If servlet do not implement the SingleThreadModel interface.
> Remeber two things that (1) any thingin doGet() or doPost() is thread safe.
Maybe, maybe not. Just because something is in doGet or doPost does not
automatically make it thread safe:
class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) {
File myfile = new File("MyLocalFile");
//do something with file
}
}
In the code above, two threads can access the File object concurrently.
Depending on what each thread is doing, the two threads could interfere
with each other.
> (2) For any Non LOcal variable of servlet
> you have to use the concept of synchronize
Maybe, maybe not. Just because you use a member variable does not mean
you must synchronize:
class MyServlet extends HttpServlet {
public String myString = "Is this thread safe?";
public void doPost(HttpServletRequest req, HttpServletResponse res) {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>);
out.println(myString);
out.println("</body></html>");
}
}
In the code above, every thread uses the same class member String
object. Yet no thread will interfere with any other thread because no
thread tries to modify myString. A problem would occur if any thread
tried to modify myString.
For thread-safety:
1) Use local variables
2) Use member variables only for that information which is truly a
member of the servlet
3) If the member variables are modified by a thread, synchronize or
otherwise protect access to those members
4) If the servlet uses any system resources, synchronize or otherwise
protect access to those resources
Kevin Mukhar
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html