[concurrency-interest] Thread safe or not ?

Hanson Char hanson.char at gmail.com
Thu Oct 6 00:51:21 EDT 2005


Yes, thanks. Indeed the better solution you point out has also been pointed
out by Joe Bowbeer in an earlier response to this thread.

Hanson

On 10/5/05, Dawid Kurzyniec <dawidk at mathcs.emory.edu> wrote:
>
> Hanson Char wrote:
>
> > That's exactly right - very astute observation as usual. It's an
> > outline of a pattern for initializing, in a thread-safe manner,
> > multiple final static constants via (optionally) some wiring of
> > external configuration.
> >
> > Clients of the final static constants don't need to worry about the
> > external configuration. The wiring framework (such as Spring) doesn't
> > need to concern about the external configuration actually ends up in a
> > bunch of static final constants.
> >
> > In the trivial example I gave there is only one static final constant,
> > K. However, imagine we have multiple system constants. A
> > SystemParameterBean instance can be initialized once, and can then be
> > used to initialize all the static final constants in SystemParameter,
> > which are then accessed by clients in a simple way. Like
> > SystemParameter.K, SystemParameter.J, etc.
> >
> > In such multiple-constant scenario, we can avoid init().getK() and
> > init().getJ(), etc. but simply bean.getK() and bean.getJ(). The
> > init() method implementation, which may incur additional overheads,
> > should be done only once and not linear to the number of system
> constants.
> >
> I apologize for such a delay in responding, but, if you move init() to
> the static initializer, you can make the bean a local variable,
> obviating the need to store it in a static field even though you access
> it more than once:
>
> public class SystemParameter {
> public static final long K;
> public static final long L;
> ...
> static {
> SystemParameterBean bean = init();
> K = bean.getK();
> L = bean.getL();
> ...
> }
>
>
> Regards,
> Dawid
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20051005/fecff49b/attachment.htm


More information about the Concurrency-interest mailing list