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

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

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

    lqxue

    常用鏈接

    統(tǒng)計(jì)

    book

    tools

    最新評(píng)論

    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 閱讀(245) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java

    主站蜘蛛池模板: 免费播放一区二区三区| 97人妻精品全国免费视频| 久久亚洲精品国产精品黑人| 免费无码又爽又刺激高潮的视频| h在线看免费视频网站男男| 爱情岛亚洲论坛在线观看| 亚洲av午夜精品无码专区| 精品亚洲综合久久中文字幕| 一区国严二区亚洲三区| 免费一级黄色毛片| 亚洲AV无码之日韩精品| 免费va人成视频网站全| 日本一道本高清免费| 免费看国产一级片| 免费欧洲美女牲交视频| 大陆一级毛片免费视频观看| 成人毛片18岁女人毛片免费看| 国产免费av片在线看| 国产精品无码免费视频二三区| 午夜小视频免费观看| 亚洲国产精品尤物YW在线观看| 亚洲男人av香蕉爽爽爽爽| 亚洲精品成人无码中文毛片不卡| 亚洲欧洲无码AV电影在线观看| 亚洲Aⅴ无码专区在线观看q| 亚洲精品美女久久久久9999| 97se亚洲国产综合自在线| 国产亚洲人成在线影院| 国产免费一区二区三区不卡| 精品国产无限资源免费观看| 日本最新免费不卡二区在线| 久久精品国产亚洲AV不卡| 亚洲网站视频在线观看| 美女视频黄.免费网址| 久久国产色AV免费观看| 国产在线观看免费不卡| 亚洲综合精品一二三区在线| 国产精品亚洲av色欲三区| 免费无码VA一区二区三区 | 中文字幕中韩乱码亚洲大片| 亚洲中文久久精品无码1 |