For starters, if one thread invokes setName
while access to name
isn't somehow synchronized, there's no guarantee that any other thread will ever see that new value.
Second, String.valueOf(char[])
iterates over name
. That means if even one of name
's characters is set during this iteration, the iterating thread might see inconsistent data - the first characters of the original char array and the last characters of the other.
In this particular case, it's not one of characters, rather it's the pointer to the beginning of the char array. Assuming that an iteration over an array computes the next cell to access by adding the current iteration index to that pointer's referred address, it will indeed also cause inconsistent data.
** EDIT **
Regarding the second non-safe scenario, after reading this question and answer, it seems much less obvious as to how actually an array copy is implemented - it is platform-dependent. This explains why String,valueOf(char[])
isn't documented as thread-safe. So, anyway, the second scenario still applies as non-thread-safe.