On Feb 20, 2012, at 8:25 , Frank Durden wrote: > I'm sorry if this is explained somewhere, I couldn't find an answer. > Are protobuf messages (in Java) thread safe for concurrent reads. I > guess they're immutable in the sense that you can't modify them after > they're built, but can a message object content be read from different > threads safely? The generated variables in message objects don't seem > to be final or volatile?
After you call .build() and get a Message, that message is immutable, as you observed. I'm not a Java memory model expert, but my understanding is that despite the fields not being market final, this is in fact thread-safe. However, my only support is this quote from Brian Goetz: "With some additional work, it is possible to write immutable classes that use some non-final fields (for example, the standard implementation of String uses lazy computation of the hashCode value), which may perform better than strictly final classes." http://www.ibm.com/developerworks/java/library/j-jtp02183/index.html I'm pretty sure the right people at Google have examined the protobuf code, so it should be safe. However, I don't have a good argument for *why* it is safe. Maybe someone who is a Java memory model expert knows the reasoning here? Evan -- 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.
