I appreciate all of the great input. Thanks!!!

--- On Mon, 6/15/09, Jonathan Mast <jhmast.develo...@gmail.com> wrote:

From: Jonathan Mast <jhmast.develo...@gmail.com>
Subject: Re: using static helper classes within servlets
To: "Tomcat Users List" <users@tomcat.apache.org>
Date: Monday, June 15, 2009, 2:32 PM

Sid, what everyone is saying about my first reply to your post is correct,
that code sample unnecessarily uses synchronization to achieve what static
initialize could do less expensively.  I wrote class awhile ago with paying
much attention to the costs of synchronization.

private static final SomeBean someBean = new SomeBean();

public static getSomeBean() {
   return someBean;
}

is a much better approach to the issue you are having and I will myself
transition to this pattern the shortly.

I should add that in my webapp, most references to Beans served by the lazy
BeanBag are themselves statically initialized (because static methods make
use of them) so this has probably shielded me from the costs of the accessor
methods being synchronized.


On Mon, Jun 15, 2009 at 1:25 PM, Caldarale, Charles R <
chuck.caldar...@unisys.com> wrote:

> > From: Christopher Schultz [mailto:ch...@christopherschultz.net]
> > Subject: Re: using static helper classes within servlets
> >
> > You can get a significant performance improvement by doing this
> > instead:
> >
> > private static final SomeBean someBean = new SomeBean();
> >
> > public static getSomeBean() {
> >   return someBean;
> > }
> >
> > Of course, if you're going for delayed/lazy instantiation, you're not
> > going to get it, but you at least drop the penalty for synchronization.
>
> Using an initialize-on-demand holder class permits lazy instantiation:
>
> class MyClass {
>  ...
>  private static class LazySomethingHolder {
>    public static final Something something = new Something();
>  }
>  ...
>  public static Something getInstance() {
>    return LazySomethingHolder.something;
>  }
>  ...
> }
>
> The LazySomethingHolder will be loaded - but not initialized - when MyClass
> is loaded.  Initialization will occur only when MyClass.getInstance() is
> invoked.  The necessary synchronization during initialization is all handled
> internally by the JVM.
>
>  - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you received
> this in error, please contact the sender and delete the e-mail and its
> attachments from all computers.
>
>



      

Reply via email to