<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Raymond
    Java筆記

    Volatile Fields

    Sometimes, it seems excessive to pay the cost of synchronization just to read or write an instance field or two. After all, what can go wrong? Unfortunately, with modern processors and compilers, there is plenty of room for error:

    • Computers with multiple processors can temporarily hold memory values in registers or local memory caches. As a consequence, threads running in different processors may see different values for the same memory location!

    • Compilers can reorder instructions for maximum throughput. Compilers won't choose an ordering that changes the meaning of the code, but they make the assumption that memory values are only changed when there are explicit instructions in the code. However, a memory value can be changed by another thread!

    If you use locks to protect code that can be accessed by multiple threads, then you won't have these problems. Compilers are required to respect locks by flushing local caches as necessary and not inappropriately reordering instructions. The details are explained in the Java Memory Model and Thread Specification developed by JSR 133 (see http://www.jcp.org/en/jsr/detail?id=133). Much of the specification is highly complex and technical, but the document also contains a number of clearly explained examples. A more accessible overview article by Brian Goetz is available at http://www-106.ibm.com/developerworks/java/library/j-jtp02244.html.

    NOTE

    Brian Goetz coined the following "synchronization motto": "If you write a variable which may next be read by another thread, or you read a variable which may have last been written by another thread, you must use synchronization."


    The volatile keyword offers a lock-free mechanism for synchronizing access to an instance field. If you declare a field as volatile, then the compiler and the virtual machine take into account that the field may be concurrently updated by another thread.

    For example, suppose an object has a boolean flag done that is set by one thread and queried by another thread. You have two choices:

    1. Use a lock, for example:

      public synchronized boolean isDone() { return done; }
      private boolean done;
      

      (This approach has a potential drawback: the isDone method can block if another thread has locked the object.)

    2. Declare the field as volatile:

      public boolean isDone() { return done; }
      private volatile boolean done;
      

    Of course, accessing a volatile variable will be slower than accessing a regular variablethat is the price to pay for thread safety.

    NOTE

    Prior to JDK 5.0, the semantics of volatile were rather permissive. The language designers attempted to give implementors leeway in optimizing the performance of code that uses volatile fields. However, the old specification was so complex that implementors didn't always follow it, and it allowed confusing and undesirable behavior, such as immutable objects that weren't truly immutable.


    In summary, concurrent access to a field is safe in these three conditions:

    • The field is volatile.

    • The field is final, and it is accessed after the constructor has completed.

    • The field access is protected by a lock.

    posted on 2006-02-19 15:58 Raymond的Java筆記 閱讀(380) 評論(0)  編輯  收藏 所屬分類: Java
     
    主站蜘蛛池模板: 亚洲成a人片在线看| 亚洲综合国产精品| 亚洲熟妇无码AV| 久久精品免费一区二区喷潮| 亚洲熟妇色自偷自拍另类| 91视频免费网址| 亚洲精品亚洲人成在线麻豆| 24小时日本韩国高清免费| 亚洲今日精彩视频| 亚洲一区免费在线观看| 亚洲国产高清视频在线观看| 国产免费看JIZZ视频| 亚洲人成未满十八禁网站| 日本一线a视频免费观看| 无遮挡a级毛片免费看| 亚洲午夜激情视频| a毛片免费全部播放完整成| 国产亚洲人成无码网在线观看| 很黄很污的网站免费| 亚洲毛片一级带毛片基地| 日产乱码一卡二卡三免费| 日本一区二区在线免费观看 | 小说区亚洲自拍另类| 四虎成人精品在永久免费| 丰满少妇作爱视频免费观看| 亚洲人成网亚洲欧洲无码久久| 久久永久免费人妻精品下载| 国产精品亚洲专区在线观看| 亚洲第一区精品日韩在线播放| 美女网站在线观看视频免费的| 亚洲专区在线视频| 国产男女性潮高清免费网站 | 77777亚洲午夜久久多人| 无码中文字幕av免费放dvd| 亚洲国语在线视频手机在线| 又黄又爽一线毛片免费观看| a毛片免费观看完整| 亚洲国产精品无码久久98 | 久久九九久精品国产免费直播| 91亚洲导航深夜福利| 国产午夜免费秋霞影院|