jvm_book icon indicating copy to clipboard operation
jvm_book copied to clipboard

P449

Open GawinHong opened this issue 4 years ago • 1 comments

周老师,你好 P449书中原文(规则1

只有当线程T对变量V执行的前一个动作是load的时候,线程T才能对变量V执行use动作;并且,只有当线程T对变量V执行的后一个动作是use的时候,线程T才能对变量V执行load动作。线程T对变量V的use动作【可以认为】是和线程T对变量V的load、read动作相关联的,必须【连续】且一起出现

我觉得这段话最后一句表达方式容易歧义,建议去掉【可以认为】

按这段话,依据最后【一句话】的信息,我可以这样理解:volatile变量的read->load->use三个操作必须【连续】一起出现,即read->load中间也是不可插入其他指令。(记为结论1

但是P443中原文提到过

Java内存模型只要求上述两个操作必须按顺序执行,但不要求是连续执行。也就是说read与load之间、store与write之间是可插入其他指令的

P443这些信息得知,正常情况下read->load中间是可以插入其他指令的(记为结论2

但是书中P449这段话的第一句,只能得出【load->use】是必须连续的,第二句话说【可以认为】,感觉是第一句话推导出来的,而不是Java内存模型里面直接规定的,但其实是推导不出来的,因为结论2,所以我认为【可以认为】这四个字是不是可以去掉,包括第二条规则的表述(规则2

只有当线程T对变量V执行的前一个动作是assign的时候,线程T才能对变量V执行store动作;并且,只有当线程T对变量V执行的后一个动作是store的时候,线程T才能对变量V执行assign动作。线程T对变量V的assign动作【可以认为】是和线程T对变量V的store、write动作相关联的,必须连续且一起出现。

我最终的看法是 规则1说明的是:read->load->use三个操作必须【连续】 规则2说明的是:assign->store->write三个操作必须【连续】

原因: 规则2必须满足否则规则3无法满足(我列举过规则3的AB对应的所有情况,规则2必须是成立的)

举例: 如果 A动作是 assign assign V-> store V-> write V B动作也是是 asssign assign W-> store W-> write W

如果A先于B,假设规则2不成立 可能就有 assign V【A】-> store V-> assign W【B】-> store W-> write W【Q】-> write V【P】

看出P是晚于Q的,"那么P先于Q"不成立,所以规则3要成立,规则2必须要成立的,这样store V和write V直接就不会插入assign W的操作

规则1的表述和规则2的【表述方式】一样,所以我认为规则1也是同样也是成立的。

所以其实里面暗含着volatile变量有特殊规定read->load之间不可以插入其他指令,store->write也一样,用【可以认为】会让人因为P443说到的问题感到困惑

GawinHong avatar Nov 05 '20 13:11 GawinHong

你好。由于主贴的表述确实有点拗口,我不能确认是否准确理解了你所说的意思。

请先看下这段文字的原始材料来源,12.3小节开篇的脚注中有提到,它来自于JVMS6的“Threads and Locks”一节,这里给出较旧的版本是因为JMM相关的内容在JVMS7之后就移出去,以独立JSR形式存在了,但内容并未变化。

Rules for volatile Variables If a variable is declared volatile, then additional constraints apply to the operations of each thread. Let T be a thread and let V and W be volatile variables.

  • A use operation by T on V is permitted only if the previous operation by T on V was load, and a load operation by T on V is permitted only if the next operation by T on V is use. The use operation is said to be "associated" with the read operation that corresponds to the load.
  • A store operation by T on V is permitted only if the previous operation by T on V was assign, and an assign operation by T on V is permitted only if the next operation by T on V is store. The assign operation is said to be "associated" with the write operation that corresponds to the store.
  • Let action A be a use or assign by thread T on variable V, let action F be the load or store associated with A, and let action P be the read or write of V that corresponds to F. Similarly, let action B be a use or assign by thread T on variable W, let action G be the load or store associated with B, and let action Q be the read or write of W that corresponds to G. If A precedes B, then P must precede Q. (Less formally: operations on the master copies of volatile variables on behalf of a thread are performed by the main memory in exactly the order that the thread requested.)

将“is said to be”翻译为“可以认为”是否妥当,作为一个语文问题先暂时搁置。后一句的“连续且一起”也是有前提“线程T对变量V的”的,并不推导出中间不能插入指令的结论。

我认为以上并不是理解偏差的关键,关键是: volatile特殊规定的关注点不在于中间能不能插入别的指令,它强调的是说每一个use操作都要对应一个read/load操作,即不允许把volatile变量中read/load到工作内存后,反复地多次use给执行引擎,而对于普通变量是允许这样做的。

fenixsoft avatar Nov 06 '20 03:11 fenixsoft