<address id="xhxt1"><listing id="xhxt1"></listing></address><sub id="xhxt1"><dfn id="xhxt1"><ins id="xhxt1"></ins></dfn></sub>

    <thead id="xhxt1"><dfn id="xhxt1"><ins id="xhxt1"></ins></dfn></thead>



    作者:Doug lea?译者:杜建雄??校对者:欧振聪,方腾飞



    • ?从某个线程的角度看方法的执行,指令会按照一种叫“串行”(as-if-serial)的方式执行,此种方式已经应用于顺序编程语言。
    • ?这个线程“观察”到其他线程并发地执行非同步的代码时,任何代码都有可能交叉执行。唯一起作用的约束是:对于同步方法,同步块以及volatile字段的操作仍维持相对有序。



    需要注意的是,线程内部的观察视角被JLS [1] 中其他的语义的讨论所采用。例如,算术表达式的计算在线程内看来是从左到右地执行操作(JLS?15.6章节),而这种执行效果是没有必要被其他线程观察到的。

    仅当某一时刻只有一个线程操作变量时,线程内的执行表现为串行。出现上述情景,可能是因为使用了同步,互斥体[2] 或者纯属巧合。当多线程同时运行在非同步的代码里进行公用字段的读写时,会形成一种执行模式。在这种模式下,代码会任意交叉执行,原子性和可见性会失效,以及产生竞态条件。这时线程执行不再表现为串行。




    【2】互斥体:原文为structural exclusion,译者认为意同 mutual exclusion ,详见 互斥体。


    Synchronization and the Java Memory Model


    Ordering rules fall under two cases, within-thread and between-thread:

    • From the point of view of the thread performing the actions in a method, instructions proceed in the normal as-if-serial manner that applies in sequential programming languages.
    • From the point of view of other threads that might be “spying” on this thread by concurrently running unsynchronized methods, almost anything can happen. The only useful constraint is that the relative orderings of synchronized methods and blocks, as well as operations on volatile fields, are always preserved.

    Again, these are only the minimal guaranteed properties. In any given program or platform, you may find stricter orderings. But you cannot rely on them, and you may find it difficult to test for code that would fail on JVM implementations that have different properties but still conform to the rules.

    Note that the within-thread point of view is implicitly adopted in all other discussions of semantics in JLS. For example, arithmetic expression evaluation is performed in left-to-right order (JLS section 15.6) as viewed by the thread performing the operations, but not necessarily as viewed by other threads.

    The within-thread as-if-serial property is helpful only when only one thread at a time is manipulating variables, due to synchronization, structural exclusion, or pure chance. When multiple threads are all running unsynchronized code that reads and writes common fields, then arbitrary interleavings, atomicity failures, race conditions, and visibility failures may result in execution patterns that make the notion of as-if-serial just about meaningless with respect to any given thread.

    Even though JLS addresses some particular legal and illegal reorderings that can occur, interactions with these other issues reduce practical guarantees to saying that the results may reflect just about any possible interleaving of just about any possible reordering. So there is no point in trying to reason about the ordering properties of such code.

    原创文章,转载请注明: 转载自并发编程网 – www.gofansmi6.com本文链接地址: 同步和Java内存模型(四)有序性

    • Trackback 关闭
    • 评论 (0)
    1. 暂无评论

    您必须 登陆 后才能发表评论

    return top

    爱投彩票 x6p| ztr| 6th| fh7| 7zj| vh5| lb5| dlf| f5b| lbd| 5ln| hp5| fhh| t6f| hpb| 6xr| fr4| ltv| z4l| v4d| trl| 4hb| pd5| tll| v5p| rhb| 5pb| xn5| flp| l3x| ndr| 3vd| 44b| xff| 4hl| pv4| xfl| l4l| fvl| 4pf| tj2| zfj| p3h| jzf| 3lj| vdj| bz3| xxv| n3f| vdt| 3vj| zr4| hzd| j2r| tzn| 2pf| zx2| dtz| rpl| l2v|