[concurrency-interest] Thread safe or not ?

Joe Bowbeer joe.bowbeer at gmail.com
Fri Sep 23 15:02:31 EDT 2005


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;
>

I was assuming there was some arbitrary amount of initialization
requiring a static block.

But if the bean instance is going to be nulled in the static block
anyway, then clearly all of the initialization can be moved into the
static block, thereby avoiding the confusing (and suspect)
instantiation in a static field and subsequent null assignment in the
static block.


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;
> > > >     }
> > > > }
> > > >



More information about the Concurrency-interest mailing list