Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

From oracle docs:

"A class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value."

Looks thread safe to me. No setters on the object.



Go look at the implementation...


http://grepcode.com/file/repository.grepcode.com/java/root/j...

I hope this has been fixed in subsequent releases!


SecureRandom is thread safe, and the global initialization is volatile. What am I missing?


The `volatile_var = local_var = X` seems safe - even if you break it into two assignments it only uses the local var afterward, so SecureRandom should be fully initialized in that thread. And though SecureRandom is non-final, it does look safe to use with double-initialization - it self-seeds if not explicitly seeded (it's not in this case), so even double-init shouldn't repeat a value (which is as strong of a statement as SecureRandom allows here - no idea if it makes that claim!).

So I'd think it only matters if `volatile` doesn't guarantee that non-final object fields are fully initialized... and I can't find anything that explicitly states one way or another, so I'm not sure. If it doesn't make that guarantee, then yeah - this could publish a partly-initialized SecureRandom without a generator, which would probably crash. But the contents of https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.htm... imply to me that write-then-read is equivalent to a synchronized block or any other monitor sequence (and in the method they only use the local var, so it doesn't matter for the creator-thread), so I suspect it's fine. Just can't claim any further.

@exabrial: care to elaborate? What's unsafe / have you seen crashes due to uninitialized SecureRandom?


Keep in mind that data races in Java are not undefined behaviour. There are quite a few places where authors of standard library deliberately left out synchronization, for example in String.hashCode.

EDIT: In this case numberGenerator is volatile so it introduces happens-before relations between write and read.


Before you downvote me, make sure your reasoning is correct. I am taking about official jdk from Orale, not open jdk. See, this was fixed http://bugs.java.com/view_bug.do?bug_id=6611830


6b14 is actually damn old. The Java 6 VM is not supported anymore and in fact on the latest 7 or 8 it's definitv fixed for OpenJDK and Oracle JDK.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: