Skip to main content

question about visibility of jmm(java memory model) or jsr133

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
5 replies [Last post]
squallwang
Offline
Joined: 2014-07-15

hi all,

i had a question about visibility of jmm and which confused me a long time ,question discriber as follow :

as we know ,this is 6 actions defined when we read/write share variable between main momery and work copy of thread ,we just think work copy as cache,

we know when thread read a share variable ,the thread need to copy variable from monery to cache, "read-load-use", if the thread read the same variable manytime ,this is two different deal maybe use this variable from cache "use",or read variable from momery again "read-load-use" which decided by jvm,

an other way when thread write a variable , frist copy variable from memory to cache and write variable "assign-store-write",when
the thread write the same varibale manytime ,this is still two situation decided by jvm, write varible in cache manytime and fiush last
resulte to momony "assign",or write cahe and flush to memory again and again, “assgin-store-load”,

my question is we image this scenairo,the single thread read and write a share varibel (the variable in memory ) again and again,

how to jvm controll this situation, the thread write the varibale in cache ,but hadnot flush this variable to memory ,and this time thread read this memory but from memory ,beacuse of the new variable had‘t flush to memory,why this single thread know the variable had been changed and can read a correct value ?

thanks all,

best regard !

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sensible
Offline
Joined: 2014-07-18

This post was slightly hard to follow, but if I understood you correctly, here is my response:

First of all, some processors exhibit a strong memory model. These processors see exactly the same value for any given memory location at all times. So a single thread can always know when a variable has been changed and can read this value.

However, processors with weak memory models need special instructions call memory barriers. These barriers flush the local processor cache in order to see writes made by other processors or make writes by this processor visible to others.

As I understand it, when a variable is created it is stored to main memory and cache is flushed immediately to give that variable visibility in the particular thread. This is done asynchronously unless otherwise specified. Once it is in main memory and cache has been flushed, it can be pulled from that same location as many times as you would like. If it is changed, the variable will be updated in the same memory cell.

Hope this helped.

squallwang
Offline
Joined: 2014-07-15

Thanks for your reply patiently,it's very useful for me,but there is something that still confuse me, I wonder if you can help me out, i would appreciate it.

I understand what you mean and we know the multiprocessor computer can cache consistently because of some special protocol like MESI and so on,since like this,why when we coding about threads,we still need synchronize excipity.For my understanding,the reason for this phenomenon maybe caused by this,when the thread need to write a variable,need assign - store - write actions,each of those action is atomic,but
"assign - store - write" is not atomic and maybe reording,is it right?

thanks,
best regard~

squallwang
Offline
Joined: 2014-07-15

Thanks for you solution patient,its very useful for me,but this is something still confused me,could you help me please,

I understand your mean and we know the multiprocessor computer can cache consistently because of some special protocol like MESI and so on,since like this,why when we coding about threads,we still need synchronize excipity.For my understanding,the reason why about this phenomenon maybe caused by this,when the thread need to write a variable,need assign - store - write actions,each of those action is atomic,but
"assign - store - write" is not atomic and maybe reording,is it right?

thanks,
best regard~

sensible
Offline
Joined: 2014-07-18

Are you referring to volatile or to synchronization? We need synchronization to ensure that no other threads will run while the synchronized thread is running. Otherwise they will just overlap and run on top of each other.

Java guarantees that reading or writing a variable is atomic unless the variable is "long" or "double". These types need to be written in two operations: one that writes the first 32 bits and the second that writes the last 32 bits (because of their length). The problem with this is that when other threads are reading the value they might obtain the "intermediate value" or the value when it's only half written. This is why keywords like "volatile" or "AtomicLong" is used to ensure that the value is read as a single, atomic operation.

squallwang
Offline
Joined: 2014-07-15

Thanks for you reply and there is still something i confused.

1.In your parper,you refer to strong memory model and weak memory models,in my opinion,i know java is weak memory models about sequential consistency.so in the multithread program,we need synchronize,but on multiprpcessor,java memory model can guarantee cache coherency?beacuse almost all of multiprpcessor can guarantee cache coherency under some special protocol.

2,according to your reply ,"read and write is atomic in java",we know when java "write“ a variable ,the "write" operation including follow step,assgin,store,write,you means the hold write operation is atomic or write step in write operation is atomic?

3.Maybe i confused some concepts.what's the different between cache coherency and cache consistency? whether the cache consistency is a wrong view or the cache consistency equals to sequential consistency in java?

4.what 's the different between cache in CPU and cache in java.

Thanks ,
Best regard!