On Sunday, March 13, 2011 11:10:25 PM UTC+1, Graham Allan wrote:
>
> It's a common misconception that a class has to be final to be immutable, 
> as 
> described in Effective Java, it actually just needs to prevent subclassing.
>

Right, but interfaces cannot prevent subtyping by non-friends unless the 
interface is itself package private.
 

> Yes, this is... hard. String has what's known as a 'benign data race': 
> when 
>
> hashCode() is executed by different threads, there is a possibility that 
> the 
> hashCode field is computed and set twice, but since the result depends on 
> only 
> other immutable state, it doesn't matter, since they will always get the 
> same 
> result. While I don't think it's been proved that detecting this is 
> impossible 
> with static analysis, I've pretty much given up on trying to recognise such 
> a 
> pattern from the bytecode alone.
>

Yes. Blast those practical concerns ;P

It's yet more ammo for the argument that @Immutable gets you some light 
compiler analysis which you are free to @SuppressWarnings if you know 
better, such as the implementors of string 'knowing better' about its cached 
hashCode seemingly being not immutable and in fact seemingly a race bug.

> P.S. while I've made reference to a tool I released which is relevant to 
> this 
> discussion, I wasn't sure if it was bad form to promote it here. So I'll 
> wait 
> to see if anyone's interested to hear about it, in which case I'll happily 
> link to it.
>

Gimme :)
 

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to