concurrency - Scala immutable collections cannot be shared without synchronization? -


from «learning concurrent programming in scala» book:

in current versions of scala (2.11.1), however, collections deemed immutable, such list , vector, cannot shared without synchronization. although external api not allow modify them, contain non-final fields.

could demonstrate small example? , still apply 2.11.7?

the behavior of changes made in 1 thread when viewed governed java memory model. in particular, these rules extremely weak when comes building collection , passing built-and-now-immutable collection thread. jmm not guarantee other thread won't see earlier view collection not built!

since synchronized blocks enforce ordering, can used consistent view if they're used on every single operation.

in practice, though, necessary. on cpu side, there typically memory barrier operation can used enforce memory consistency (i.e. if write tail of list , pass memory barrier, no other thread can see tail un-set). , in practice, jvms have implement synchronized using memory barriers. 1 hope pass created list within synchronzied block, trusting memory barrier issued, , thereafter fine.

unfortunately, jmm doesn't require implemented in way (and can't assume memory-barrier-like behavior of object creation full memory barrier applies in thread opposed final fields of object), both why recommendation is, , why it's not fixed (yet, anyway) in library.

for it's worth, on x86 architectures, i've never observed problem if hand off immutable object within synchronized block. have observed problems if try cas (e.g. using java.util.concurrent.atomic classes).


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -