According to the Java Language Specification [JLS 2011], §8.3.1.4, "volatile
Fields" [JLS 2011],"
A field may be declared
volatile
, in which case the Java Memory Model ensures that all threads see a consistent value for the variable (§17.4).
...
This noncompliant code example attempts to use the volatile-read, synchronized-write technique described by Goetz in Java Theory and Practice [Goetz 2007]. The map
field is declared volatile to synchronize its reads and writes. The put()
method is also synchronized to ensure that its statements are executed atomically.
...
Because DateFormat
is not thread-safe [API 2011], the value for Date
returned by the parse()
method might fail to correspond to the str
argument.:
Compliant Solution (Instance per Call/Defensive Copying)
...
...