----- Original Message -----
From: "Peters, Jim" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, November 13, 2001 2:33 PM
Subject: Utility class and multi-threaded servlets


> I searched the archives for an answer to my question, but did not see one
> posed exactly like mine.
>
> I am developing a multi-threaded servlet app, and have developed a utility
> class (all static methods) that all the servlets use.
> For example:
>
> public class Utility
> {
>    public static String convertDouble(double val)
>    {
>       NumberFormat nf = NumberFormat.getInstance();
>       nf.setMaximumFractionDigits(2);
>       nf.setMinimumFractionDigits(2);
>       return nf.format(val);
>    }
> }
>
> The above is used in my servlets by invoking the method in this manner:
>
> String str = Utility.convertDouble(setUp);
>
> My question is this ... Do I have to synchronize the static methods in my
> Utility class ?  Will one thread stomp on another one in the event that
> multiple are executing the code at the same time ?
>
> I suspect the answer is yes but I am also worried about all the threads
> waiting for synchronized methods.
>
> Thanks in advance and I apologize if this is too basic for this forum ...
> maybe I'm brain dead today.
>
> Jim Peters
> EDS/Michigan Solution Centre
> 901 Tower Drive, Mail Stop 1084
> Troy, MI 48098
> * 248.265.4637
> * [EMAIL PROTECTED]
>[...]


I also think you don't need to use "synchronized" here(I did a simple test)
when you use "Utility.convertDouble(setUp)" in YourServlet(a multi-thread
enviroment),  but one of the reasons I can do it without "synchronized" is
because the implementation of java.text.NumberFormat::getInstance() :

private static NumberFormat getInstance(Locale desiredLocale,
                                           int choice)
{
   ...
   return new DecimalFormat(numberPatterns[choice],
            new DecimalFormatSymbols(desiredLocale));
}

i.e., it returns a new object every time when I invoke getInstance(),
this implementation guarantee the "stateless" property.

another reason is you set  nf  with the same property/parameter(2):
    nf.setMaximumFractionDigits(2);
    nf.setMinimumFractionDigits(2);
i.e., you didn't adjust the  property of nf according to the
input-parameter "val"

so if you keep other condition(still static...),  but(in a worse case):
  - you replace NumberFormat with another class
  - and the method "getInstance()" of this class is a SingleTon
    pattern, or a "recycling/reusing/pool" pattern
  - and you set diferent property according the input-parameter, for ex.,
     if(val<1.0){
        nf.setMaximumFractionDigits(5);
       nf.setMinimumFractionDigits(5);
     }else{
        nf.setMaximumFractionDigits(2);
        nf.setMinimumFractionDigits(2);
    }

then perhaps:
 - sometimes you will get the same object for more than one thread
 - and if now those threads have a intermixing action which destroy
   the atom-property of the following 3 lines:
    1-1   nf.setMaximumFractionDigits(2);
    1 -2  nf.setMinimumFractionDigits(2);
    2       return nf.format(val);

then the result will be not always right.


Bo
Nov.13, 2001

___________________________________________________________________________
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