-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Jonathan,
On 6/14/2009 1:01 PM, Jonathan Mast wrote:
> class BeanBag {
> private static SomeBean someBean = null;
>
> public static synchronized getSomeBean() {
> if (someBean == null) someBean = new SomeBean();
> return someBean;
> }
> }
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.
> I have now numerous Servlets, JSPs and POJOs that use BeanBag to obtain
> singleton instances of my beans. Its worked great for me.
Remember that object instantiation isn't really a big deal. If you have
complex object /initialization/, then it may be worth using a
"singleton" (virtually impossible in the webapp realm unless you are
very careful), flyweight, pool, or other pattern to re-use objects yet
still retain a high degree of throughput.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAko2dsMACgkQ9CaO5/Lv0PD2/ACggkuH76invGEJOWIV2JArvNrG
W3oAmQEB3nzbXTsyE44NOTZftOfDjl7N
=xDwO
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]