[concurrency-interest] Thread safe or not ?

Hanson Char hanson.char at gmail.com
Fri Sep 23 09:13:20 EDT 2005


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.

For the lack of a better name, maybe we can call this "Static Final Constant
Initialization" pattern ?

Hanson

On 9/23/05, David Holmes <dholmes at dltech.com.au> wrote:
>
> I had presumed the code was an outline of something more elaborate and it
> was the "pattern" of initialization that was significant - otherwise it can
> be reduced to:
>  public static final long K = 20;
>  or if obtained from the "external" bean:
>  public static final long K = getExternalBean().getK();
>  there's no need to keep a reference to the bean. :)
>  David Holmes
>
> -----Original Message-----
> *From:* concurrency-interest-bounces at cs.oswego.edu [mailto:
> concurrency-interest-bounces at cs.oswego.edu]*On Behalf Of *Hanson Char
> *Sent:* Friday, 23 September 2005 5:24 PM
> *To:* Joe Bowbeer
> *Cc:* concurrency-interest at altair.cs.oswego.edu
> *Subject:* Re: [concurrency-interest] Thread safe or not ?
>
> I agree. That's more streamlined. Thanks!
>
> Hanson
>
> On 9/23/05, Joe Bowbeer <joe.bowbeer at gmail.com> wrote:
> >
> > I guess I'm missing the point but GC hints always make me suspicious.
> >
> > Can you assign K in the static initializer? That would be more
> > streamlined:
> >
> > public class SystemParameter {
> > public static final long K;
> > static {
> > SystemParameterBean bean = init();
> > K = bean.getK();
> > }
> > // ...
> >
> > On 9/22/05, Hanson Char <hanson.char at gmail.com> wrote:
> > > I see, so the amended code below is both thread-safe and minimal, and
> > > hopefully with the correct comment.
> > >
> > > Hanson
> > >
> > > public class SystemParameter {
> > > private static SystemParameterBean bean = init();
> > > public static final long K = bean.getK();
> > > static {
> > > bean = null; // hint to GC
> > > }
> > > private static SystemParameterBean init() {
> > > // This part can be replaced by accessing an IoC framework
> > > // such as Spring to retrieve an externally configured
> > > // SystemParameterBean instance, as long as such
> > > // instance is constructed and accessed by only 1 thread
> > > // (ie not concurrently.)
> > > SystemParameterBean bean = new SystemParameterBean();
> > > bean.setK(20);
> > > return bean;
> > > }
> > > }
> > >
> > > public class SystemParameterBean {
> > > private long k;
> > >
> > > public long getK() {
> > > return k;
> > > }
> > > public void setK(long k) {
> > > this.k = k;
> > > }
> > > }
> > >
> > >
> > > On 9/23/05, David Holmes < dholmes at dltech.com.au> wrote:
> > > >
> > > > Hanson,
> > > >
> > > > SystemParameter.K is a static final long value that is initialized
> > during
> > > static initialization from a newly constructed - and hence otherwise
> > > inaccessible - object. So as it stands it is always threadsafe to
> > access it,
> > > as static initialization is itself threadsafe.
> > > >
> > > > I don't know why the bean variable is volatile if it is only
> > accessed
> > > during static initialization. So it may not be minimal
> > synchronization.
> > > >
> > > > However the comment:
> > > > // This part can be replaced by accessing an IoC framework
> > > > // such as Spring to retrieve an externally configured
> > > > // SystemParameterBean instance.
> > > >
> > > > is not correct. If you use an externally configured bean object to
> > > initialize K then you need to be guaranteed that that beans k field
> > has been
> > > correctly set and may not be concurrently set while being read from
> > getK. As
> > > these are long values then atomicity of reads/writes does not apply
> > and you
> > > may read a garbage value.
> > > >
> > > > Hope that helps.
> > > >
> > > > David Holmes
> > > >
> > > >
> > > >
> > > > -----Original Message-----
> > > > From: concurrency-interest-bounces at cs.oswego.edu
> > > [mailto:concurrency-interest-bounces at cs.oswego.edu]On
> > > Behalf Of Hanson Char
> > > > Sent: Friday, 23 September 2005 3:19 PM
> > > > To: concurrency-interest at altair.cs.oswego.edu
> > > > Subject: [concurrency-interest] Thread safe or not ?
> > > >
> > > > Assuming JDK5, is the following code always thread safe when
> > > SystemParameter.K is concurrently accessed ? If so, is the
> > synchronization
> > > used in the code minimal ? If not, why not ?
> > > >
> > > > Thanks in advance.
> > > >
> > > > Hanson
> > > >
> > > > public class SystemParameter {
> > > > private static volatile SystemParameterBean bean = init();
> > > > public static final long K = bean.getK();
> > > > static {
> > > > bean = null; // hint to GC
> > > > }
> > > > private static SystemParameterBean init() {
> > > > // This part can be replaced by accessing an IoC framework
> > > > // such as Spring to retrieve an externally configured
> > > > // SystemParameterBean instance.
> > > > SystemParameterBean bean = new SystemParameterBean();
> > > > bean.setK(20);
> > > > return bean;
> > > > }
> > > > }
> > > >
> > > > public class SystemParameterBean {
> > > > private long k;
> > > >
> > > > public long getK() {
> > > > return k;
> > > > }
> > > > public void setK(long k) {
> > > > this.k = k;
> > > > }
> > > > }
> > > >
> > > >
> > >
> >
> > _______________________________________________
> > Concurrency-interest mailing list
> > Concurrency-interest at altair.cs.oswego.edu
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20050923/89900878/attachment-0001.htm


More information about the Concurrency-interest mailing list