[concurrency-interest] Thread safe or not ?

Dawid Kurzyniec dawidk at mathcs.emory.edu
Wed Oct 5 18:17:14 EDT 2005


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



More information about the Concurrency-interest mailing list