<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筆記 閱讀(381) 評論(0)  編輯  收藏 所屬分類: Java
     
    主站蜘蛛池模板: 国产一级特黄高清免费大片| 妞干网在线免费视频| 亚洲最大成人网色香蕉| 鲁丝片一区二区三区免费| 国产92成人精品视频免费| 婷婷亚洲综合五月天小说| 深夜a级毛片免费视频| 69天堂人成无码麻豆免费视频| 亚洲国产精品综合久久网络| 美女黄色免费网站| 国产在线jyzzjyzz免费麻豆 | 美美女高清毛片视频黄的一免费| 国产精品麻豆免费版| 羞羞漫画登录页面免费| 亚洲乱码无码永久不卡在线| 中文字幕无码一区二区免费| 亚洲视频一区二区三区| 黄瓜视频高清在线看免费下载| 亚洲av片劲爆在线观看| 1000部无遮挡拍拍拍免费视频观看| 国产成人A亚洲精V品无码 | 国产成人综合亚洲AV第一页 | 国产精彩免费视频| 亚洲中文无码mv| 亚洲精品国产精品乱码不卞 | 十八禁在线观看视频播放免费| 亚洲va国产va天堂va久久| 一区二区在线免费视频| 日本高清色本免费现在观看| 国产精品高清视亚洲精品| www.亚洲一区| 18女人水真多免费高清毛片| 亚洲精品伦理熟女国产一区二区| 亚洲国产电影av在线网址| 免费播放一区二区三区| 337p日本欧洲亚洲大胆艺术| 99久久免费精品国产72精品九九 | 成人午夜视频免费| 免费人成激情视频在线观看冫| www.亚洲日本| 国产日韩成人亚洲丁香婷婷|