Reloading of a singleton properties file syncronously.
I have the following class file which is a singleton and also relads the properties file synchronously(reload() method).
Can you pl. suggest if the following code is correct?
publicclass PropertiesFile{
protectedstatic PropertiesFile instance =null;
protected Properties m_props =null;
private String m_propsfilepath =null;
private PropertiesFile()
{
m_propsfilepath ="D:\TestPath";
}
publicstatic PropertiesFile getInstance()
{
if (instance==null)
{
synchronized (PropertiesFile.class)
{
if (instance==null)
{
instance =new PropertiesFile();
}
}
}
return instance;
}
publicsynchronized Map getMapForID(int ID)
{
Map retMap =null;
if(m_props ==null)
{
m_props =new Properties();
FileInputStream fis =new FileInputStream(m_propsfilepath + java.io.File.separator +"properties.properties");
m_props .load(fis);
fis.close();
}
String depts = m_props.getProperty((new Integer(ID)).toString());
if (depts !=null && depts.length() > 0){
retMap =new TreeMap();
String[] strarray = depts.split(",");
retMap.put("","");
for (int i = 0; i < strarray.length; i++){
retMap.put(strarray[i].trim(),strarray[i].trim());
}
}
return retMap;
}
publicvoid reload(){
synchronized (m_props){
m_props =null;
}
getMapForID(0);
}
}
Message was edited by:
ssv45324
[3603 byte] By [
ssv45324a] at [2007-11-26 14:58:05]

# 1
> I have the following class file which is a singleton
> and also relads the properties file
> synchronously(reload() method).
>
> Can you pl. suggest if the following code is
> correct?
It is incorrect. Double check locking does not work.
Just synchronize the methods.
If there is some actual reason that you need to not use synchronization then there is an alternative.
# 3
> HI,
>
> Thanks for the reply. If the methods are synchronized
> i believe this would lead to performance issues,
> because in my application i expect many thread to
> access getMapforID.
Don't guess. Performance depends on volume, usage and actual flow. So if the volume isn't high enough or the usage isn't that often or there are other things (like database access) then it won't even rise above the noise level. Not to mention that allowing for "many threads" could limit performance as well and would having nothing to do with this.
> ( I am aware that i had marked the
> method synchronized in the code, but that is a
> mistake).
>
> Can you please let me know an alternative?
You differentiate read access from write access. Your code appears to only do reads from the thread standpoint. Reads do not need to use synchronized access at all - writes is where problems show up.
So you have three scenarios
1. Startup (needs a write)
2. Normal access (just reads)
3. Reload (writes while reading occurs.)
Solutions.
1. Start up - Load using a static initializer. That will lock access at the class level.
2. Reads - Just make sure the data structure you use does not use synchronization. If you drill down on the documentation for Properties you will find that it is synchronized.
3. Use a single reference pointer (ther must be only one.) All reads use that single reference as a local reference. The reload reassigns the global value once the construction process is complete.
Psuedo code example.....
static DataStructure data;
static
{
Construct()
}
private void Construct()
{
DataStructure tempData = new DataStructure()
// Fill in tempData.
....
// The following assignment must NOT occur
// untill ALL of the data has been put into tempData.
// It must be the last step.
data = tempData
}
public Map getMapForID(int ID)
{
// The following code MUST use a local temp variable
// It must NOT use data except in this assigment statement.
DataStructure localVar = data
id = localVar.GetMap(ID);
return id;
}
public void Reload()
{
Construct();
}