Ok, I see the distinction.  Just to make it clear, if count was not used as
a part of the CounterCons class then count would need to be synchronized?
Is this correct?  I really would like to see CounterCons as a class out-side
of HttpServlet, like you said.

-----Original Message-----
From: A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]On Behalf Of
Saumont Pierre-Yves
Sent: Saturday, April 21, 2001 2:19 PM
To: [EMAIL PROTECTED]
Subject: Re: [Re: Servlets constructors]


Because the reference to the counter (an instance of the servlet) is local
to the doGet method. So, it will not be shared among the threads making
their ways through the method. What may confuse you is that count is an
instance member. As such, it is shared by all thread going through the
object being referenced by countercons. This is a reference to an object of
the same class as our servlet, but is is *not* our servlet. It's an entirely
separate object. As count is not static, it is not shared among instances of
CounterCons. Each thread going through our servlet will create its own
countercons with it's own counter. (And the servlet will just old a
reference to an object of the same class as itself. This is a very common
situation. But in the present case, it's very poor desing. If you need a
counter object, it is much preferable to create a separate class !)

Pierre-Yves

-----Message d'origine-----
De : A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]De la part de
James Wilson
Envoy� : samedi 21 avril 2001 20:17
� : [EMAIL PROTECTED]
Objet : Re: [Re: Servlets constructors]


Why doesn't that piece need to be synchronized?

-----Original Message-----
From: A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]On Behalf Of
Saumont Pierre-Yves
Sent: Saturday, April 21, 2001 4:57 AM
To: [EMAIL PROTECTED]
Subject: Re: [Re: Servlets constructors]


Just for the fun, here is the original code revisited :

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class CounterCons extends HttpServlet {

   int count;

   public CounterCons (){}

   public CounterCons (int i)
   {
        count=i;
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
        CounterCons countercons = new CounterCons (1122);
        PrintWriter out=res.getWriter();
        countercons .count++;
        out.println(countercons .count);
     }
}

(Mark, note that there is no need to synchronize !)

But wait, this is cheating. The original poster didn't want any reference
created. So here is the solution :

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class CounterCons extends HttpServlet {

    int count;

    public CounterCons (){}

    public CounterCons (int i) {
        count=i;
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
        res.getWriter().println(++((new CounterCons(1122)).count));
     }
}

Pierre-Yves

P.S. Just kidding (for those who didn't notice)

___________________________________________________________________________
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

Reply via email to