This works fine under some conditions.
- its okay to try and set the field more than once.
- its okay if individual threads see different values.
Often when you create an object which is not changed e.g. loading a Properties from disk, having more than one copy for a short amount of time is not an issue.
private static Properties prop = null;public static Properties getProperties() { if (prop == null) { prop = new Properties(); try { prop.load(new FileReader("my.properties")); } catch (IOException e) { throw new AssertionError(e); } } return prop;}
In the short term this is less efficient than using locking, but in the long term it could be more efficient. (Although Properties has a lock of it own, but you get the idea ;)
IMHO, Its not a solution which works in all cases.
Perhaps the point is that you can use more relaxed memory consistency techniques in some cases.