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

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

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

    lqxue

    常用鏈接

    統計

    book

    tools

    最新評論

    What does the transient and volatile keywords do?

    When your not sure consult the 'Bible', 'Java™ Language Specification'
    http://java.sun.com/docs/books/jls/t...ses.html#78119


    8.3.1.3 transient Fields
    Variables may be marked transient to indicate that they are not part of
    the persistent state of an object.

    If an instance of the class Point:

    class Point {
    int x, y;
    transient float rho, theta;
    }

    were saved to persistent storage by a system service, then only the
    fields x and y would be saved. This specification does not specify
    details of such services; see the specification of java.io.Serializable
    for an example of such a service.

    8.3.1.4 volatile Fields
    As described in §17, the Java programming language allows threads to
    access shared variables. As a rule, to ensure that shared variables are
    consistently and reliably updated, a thread should ensure that it has
    exclusive use of such variables by obtaining a lock that,
    conventionally, enforces mutual exclusion for those shared variables.

    The Java programming language provides a second mechanism, volatile
    fields, that is more convenient than locking for some purposes.
    A field may be declared volatile, in which case the Java memory model
    (§17) ensures that all threads see a consistent value for the variable.

    If, in the following example, one thread repeatedly calls the method one
    (but no more than Integer.MAX_VALUE times in all), and another thread
    repeatedly calls the method two:

    class Test {
    static int i = 0, j = 0;
    static void one() { i++; j++; }
    static void two() {
    System.out.println("i=" + i + " j=" + j);
    }
    }

    then method two could occasionally print a value for j that is greater
    than the value of i, because the example includes no synchronization
    and, under the rules explained in §17, the shared values of i and j
    might be updated out of order.

    One way to prevent this out-or-order behavior would be to declare
    methods one and two to be synchronized (§8.4.3.6):

    class Test {
    static int i = 0, j = 0;
    static synchronized void one() { i++; j++; }
    static synchronized void two() {
    System.out.println("i=" + i + " j=" + j);
    }
    }

    This prevents method one and method two from being executed
    concurrently, and furthermore guarantees that the shared values of i and
    j are both updated before method one returns. Therefore method two never
    observes a value for j greater than that for i; indeed, it always
    observes the same value for i and j.

    Another approach would be to declare i and j to be volatile:

    class Test {
    static volatile int i = 0, j = 0;
    static void one() { i++; j++; }
    static void two() {
    System.out.println("i=" + i + " j=" + j);
    }
    }

    This allows method one and method two to be executed concurrently, but
    guarantees that accesses to the shared values for i and j occur exactly
    as many times, and in exactly the same order, as they appear to occur
    during execution of the program text by each thread. Therefore, the
    shared value for j is never greater than that for i, because each update
    to i must be reflected in the shared value for i before the update to j
    occurs. It is possible, however, that any given invocation of method two
    might observe a value for j that is much greater than the value observed
    for i, because method one might be executed many times between the
    moment when method two fetches the value of i and the moment when method
    two fetches the value of j.

    See §17 for more discussion and examples.

    A compile-time error occurs if a final variable is also declared volatile.

    --

    posted on 2008-03-06 00:35 lqx 閱讀(238) 評論(0)  編輯  收藏 所屬分類: java

    主站蜘蛛池模板: 国产精品美女久久久免费 | 亚洲高清视频在线观看| 国产精品美女久久久免费| 亚洲午夜国产片在线观看| 一区二区在线视频免费观看| 国产又大又粗又硬又长免费| 小说区亚洲自拍另类| 亚洲国产专区一区| 精品久久久久久国产免费了| 亚洲日本中文字幕一区二区三区| 精品特级一级毛片免费观看| 又大又硬又爽免费视频| 一级一级毛片免费播放| 亚洲中久无码永久在线观看同| 韩日电影在线播放免费版| 久久久久亚洲精品影视| 5555在线播放免费播放| 亚洲最大成人网色香蕉| 精品久久免费视频| 成在线人直播免费视频| 精品亚洲一区二区| 久久亚洲国产视频| 日本免费网站视频www区| 四虎影在线永久免费四虎地址8848aa| 亚洲av日韩综合一区久热| 亚洲国产aⅴ综合网| 拍拍拍无挡免费视频网站| 久久亚洲私人国产精品vA| 国产久爱免费精品视频| 国产aⅴ无码专区亚洲av| 在线观看永久免费| 国产亚洲综合久久| 亚洲国产精品VA在线观看麻豆| 亚欧人成精品免费观看| 亚洲AV无码AV男人的天堂不卡| 亚洲人成影院在线观看| 99热在线免费播放| 亚洲国产精品一区二区久久| 久久笫一福利免费导航| 一出一进一爽一粗一大视频免费的| 亚洲福利在线观看|