Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making load and getproeprty methods of java.util.Properties thread safe

I am using these statements to load java program properties at runtime.

 public static void Init(String confFile) {
        try {
            prop.load(new FileInputStream(confFile));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Same class has the following method for reading properties

 public static String GetProperty(String key) {
        return prop.getProperty(key);
    }

My question is, if I call Init and GetProperty simultaneously without the synchronized block will it cause a problem?

I read this java Properties . here it says that the getProperty method is thread safe. yes, I know this, because I have been using it with multiple threads, but now while one thread is loading new properties and others are calling the getProperty method I think it can cause problems

like image 594
Zeeshan Ali Avatar asked Dec 12 '25 17:12

Zeeshan Ali


1 Answers

Yes, it is threadsafe.

  • Properties extends Hashtable, which is threadsafe.
  • Its load() methods are synchronized
  • Its default properties are also a Hashtable
like image 58
sina72 Avatar answered Dec 15 '25 09:12

sina72



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!