[concurrency-interest] RE: Synchronization of data read by multiple threads

Jeremy Manson jmanson at cs.purdue.edu
Sun Oct 30 12:18:56 EST 2005


Ryan LeCompte wrote:
> Okay, that sounds fine. However, what about if the main thread (the
> one that instantiated Test) continues to read values that were set in
> the constructor? For example, if the main thread does:
> t.isInstantiated() later on, will it continue to return the value
> that was set in the constructor when Test was instantiated? Keep in
> mind that in this particular scenario the values that the main thread
> will read never change after being initially set in the constructor
> of Test. There is no need for making the 'instantiated' variable
> volatile in this case, right?
> 

Are you asking, "If one thread sets a field, and then later reads that 
field, do I have to do additional synchronization?"  The answer to that 
is no.  You don't need synchronization if you are only dealing with one 
thread.

By the way, if you are setting a field once, in the constructor, and 
then never changing it, then it is a good idea to make the field final. 
  All reads of final fields return the correctly constructed value, 
without synchronization (unless a reference to the constructed object 
escapes the constructor, or you are using some weird custom 
deserialization protocols).

					Jeremy


More information about the Concurrency-interest mailing list