Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4301189

JLS 8.3.1.4: contradiction in description of volatile fields

    XMLWordPrintable

Details

    • kestrel
    • generic
    • generic

    Description



      Name: laC46010 Date: 12/21/99


      There is a contradiction between two sentences of JLS 8.3.1.4 (p. 148):
          "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, method two never observes a value for j 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."

      There is a contradiction between sentences "method two never observes a
      value for j greater than that for i" and "that any given invocation of
      method two might observe a value for Ï that is much greater than the
      value observed for i". It seems to me there are two different
      meaninigs of the verb "observe". In the first sentence it might be read
      as "can see in main memory", while occurence in the second sentence
      might be understood as "have in working copy", or even "print". Such
      sense of the sentences quite coincides with the rules for volatile
      variables in section 17.7 (p. 407)

      ======================================================================

      Attachments

        Activity

          People

            gbrachasunw Gilad Bracha (Inactive)
            leosunw Leo Leo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: