Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are all Java Properties' methods fully synchronized?

I know that the Properties class is a sub-class of Hashtable. So all the inherited methods are synchronized, but what about the other methods of Properties such as store, load, etc? (Dealing specifically with Java 1.6)

like image 334
Traker Avatar asked Aug 26 '10 13:08

Traker


2 Answers

I always found the doc disclaimer misleading, specially for beginners (pardon if it is not your case).

This class is thread-safe: multiple threads can share a single Properties object without the need for external synchronization.

Even Thread-safe classes need synchronization more than you think. What is synchronized on that classes are their methods, but often a user uses this classes in a more complex context.

If you only put/get it is ok, but with some more code things get tighter:

p.putProperty("k1","abc");
p.putProperty("k2","123");
String.out.println(p.get("k1")+p.get("k2"));

This example code only prints for shure "abc123" in a multi threaded environment, if the section is a synchronized block (and even then things could get wrong).

For that reason (and of courrse performance) i prefer non thread safe classes and i get forced to think: is my program thread safe ...

like image 76
PeterMmm Avatar answered Nov 21 '22 22:11

PeterMmm


the java1.6 javadoc says:

This class is thread-safe: multiple threads can share a single Properties object without the need for external synchronization.

like image 31
Nikolaus Gradwohl Avatar answered Nov 21 '22 22:11

Nikolaus Gradwohl