I\'ve read around quite a bit but haven\'t found a definitive answer.
I have a class that looks like this:
public class Foo() {
private
No. Except if they are immutable.
The only thing they do is
Still if your attribute is mutable then it is not thread safe.
See also: Do we synchronize instances variables which are final?
It is exactly the same except they are class level.
There is nothing inherently thread safe about a final static
variable. Declaring a member variable final static
only ensures that this variable is assigned to just once.
The question of thread safety has less to do with how you declare the variables but instead relies on how you interact with the variables. So, it's not really possible to answer your question without more details on your program:
sharedData
variable?sharedData
?Using a ConcurrentHashMap only guarantees that the individual methods of the Map
are thread-safe, it doesn't make an operation such as this thread-safe:
if (!map.containsKey("foo")) {
map.put("foo", bar);
}
What is thread-safe? Sure, the initialization of the HashMap is thread-safe in the respect that all Foo's share the same Map instance, and that the Map is guaranteed to be there unless an exception occurs in the static init.
But modifying the contents of the Map is most assuredly not thread safe. Static final means that the Map sharedData can not be switched for another Map. But the contents of the Map is a different question. If a given key is used more than once at the same time you may get concurrency issues.
At this case only sharedData object is immmutable, that means only that all the time you will work with same object. But any data inside it can be changed (removed, added, etc) at any time, from any thread.
Yes, this is thread safe too. All final members of your static class will be initialized before any thread is allowed to access them.
If the static
block fails during initialization, an ExceptionInInitializerError
will be raised in the thread that first attempts initialization. Subsequent attempt to reference the class will raise a NoClassDefFoundError
.
In general, the contents of a HashMap
have no guarantee of visibility across threads. However, the class initialization code uses a synchronized
block to prevent multiple threads from initializing the class. This synchronization will flush the state of the map (and the HashMap
instances that it contains) so that they will be correctly visible to all threads—assuming that no changes are made to the map, or the maps it contains, outside the class initializer.
See the Java Language Specification, §12.4.2 for information about class initialization and the requirement for synchronization.
Aren't you are actually asking if the static initialization of sharedData
is thread safe and only executed once?
And yes, that is the case.
Of course many people here have correctly pointed out that the contents of sharedData
can still be modified.