search

Home  >  Q&A  >  body text

java - JMM为什么要规定普通写和volatile读不能重排序?

是否能重排序 第二个操作
第一个操作 普通读/写 volatile读 volatile写
普通读/写 YES NO YES
volatile读 NO NO NO
volatile写 YES NO NO

我理解的对volatile变量的重排序(编译器重排序和处理器重排序),主要目的是为了实现对volatile的写,Happend-Before对volatile的读

所以,按照我理解的话,并不需要像图表中那么严谨

天蓬老师天蓬老师2846 days ago981

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 09:43:49

    In the case of multi-threading, there is a synchronization problem between the thread working memory and the main memory. Volatile is only available immediately on the main memory

    reply
    0
  • Cancelreply