On Wed, 14 Nov 2001, Peters, Jim wrote:
> I get it ... I think. It's OK to have local vars in my static methods, for
> example:
>
> public static String getReferringScreen(HttpServletRequest req)
> {
> String referringScreen = req.getHeader("Referer");
> String reqScreenName = "";
>
> if (referringScreen != null)
> {
> int pos1 = referringScreen.lastIndexOf("/");
> int pos2 = referringScreen.lastIndexOf(".");
>
> if (pos1 != -1 && pos2 != -1 && pos2 > pos1)
> reqScreenName = referringScreen.substring(pos1 + 1, pos2);
> }
>
> return reqScreenName;
> }
>
> This method is thread safe too ? The only issues would be with static
> methods that utilize static vars ?
That method looks OK.
While it's true that local variables will be thread-safe while static
variables are potentially problematic, that's not the only
consideration when it comes to concurrency. For example, if external
resources (such as a file) are being accessed, there are also
potential problems. There are entire books written about this topic,
so it can't be summed up in a single post.
> -----Original Message-----
> From: Milt Epstein [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, November 13, 2001 3:59 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Utility class and multi-threaded servlets
>
>
> On Tue, 13 Nov 2001, Peters, Jim wrote:
>
> > 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.
> [ ... ]
>
> The answer is, it depends. That is, it depends on how the particular
> methods are written. There is nothing inherent about static methods
> that would require synchronization (i.e., they are not inherently
> thread-safe or thread-safe). The particular method you have above
> would not need to be synchronized -- the key point perhaps is that nf
> is local to the method. Now if nf had been a static class variable
> (and some of the other methods also manipulated nf as convertDouble
> does), then you would have a potential synchronization problem. So
> really this has to be looked at like any threading issue, the fact
> that static methods are involved really doesn't make a difference.
>
> Milt Epstein
> Research Programmer
> Software/Systems Development Group
> Computing and Communications Services Office (CCSO)
> University of Illinois at Urbana-Champaign (UIUC)
> [EMAIL PROTECTED]
Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]
___________________________________________________________________________
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