On Feb 20, 2012, at 16:20 , Christopher Smith wrote:
> Message objects *don't* have mutators and are conceptually a copy of the 
> relevant builder object.

Having attempted to refresh my knowledge of the Java Memory Model, I think 
there is a subtle difference between an object that has all final fields, and 
an "immutable" object like the protobuf messages. However, I don't think it 
matters in reality: As long as the message is "correctly published" to other 
threads (eg. a synchronized block, volatile reference, concurrent data 
structure), then everything is fine. Since everyone *should* be doing this 
already, Messages are safe to use across multiple threads.

Evan


PS. For language lawyers: I *think* the potential difference is as follows: 
Writes to final fields in a constructor are guaranteed to be visible to all 
threads when the constructor exits. So if you had the following:

static FinalImmutableObject someRef = ...;

Then if another thread sees a non-null value for someRef, it will correctly see 
all the values of the final fields. On the other hand, if you do this with a 
protobuf message, it *theoretically* could see a non-null value for someRef, 
but still see uninitialized or incorrectly initialized values for fields in 
someRef.

This is because this static variable is not synchronized or volatile, so there 
is no "happens-before" relationship between two threads. Thus, the reads on one 
thread *could* be reordered before the writes on the other thread. References:

http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.4
http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.5

--
http://evanjones.ca/

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" 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/protobuf?hl=en.

Reply via email to