[concurrency-interest] Thread safe or not ?

Hanson Char hanson.char at gmail.com
Fri Sep 23 01:19:19 EDT 2005


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;
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20050923/8894daca/attachment-0001.htm


More information about the Concurrency-interest mailing list