[concurrency-interest] Thread safe or not ?

Joe Bowbeer joe.bowbeer at gmail.com
Fri Sep 23 03:03:38 EDT 2005


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