multithreading - Do I need to synchronize access to immutable types in Java? -


let's have class:

class zoo {     protected string bearname;     protected double trainersalary;     protected integer monkeycount; } 

can 1 thread write these fields, , 1 read them, without requiring synchronized access zoo object?

note: these values can treated separate 1 another, doesn't matter trainersalary changed while monkeycount read.

edit:

just clarify, fields mutable; referenced objects immutable.

technically need make them final, volatile or read and write them using synchronzied guarantee reader read up-to-date value. have right now, if 1 thread writes in value, there's no guarantee thread read same value. because the reading thread may see cached valued. more multi-core cpus , various levels of cache.

a great book on java concurrency in practice.


Comments