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

java.util.Random spec problem on synchronization

XMLWordPrintable

    • b61
    • generic
    • generic

      In jdk 1.4.2, Random.next(int) is no longer declared synchronized with an introduction of sun.misc.AtomicLong. However, the code sample in method doc still refers to a synchronized method declaration. If the code sample is meant to show just the algorithem and not about whether the method should be declared synchronized or not (which it shouldn't state - the implementation should be able to choose to be thread safe without making the method synchronized, as in 1.4.2) then the doc should be worded better.

      /java/re/j2se/1.4.2/archive/fcs/ws/j2se/src/share/classes/java/util/Random.java

      line 94:
           * The method <tt>next</tt> is implemented
           * by class <tt>Random</tt> as follows:
           * <blockquote><pre>
           * synchronized protected int next(int bits) {
           * seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
           * return (int)(seed >>> (48 - bits));
           * }</pre></blockquote>
           <snap> */
          protected int next(int bits) {...

      There are a few more method sample codes in the Random class doc that explicitly have the synchronized method declaration. Perhaps the implementation should be able to choose how to be thread safe on those methods, too.

      Also, there is a line in Random.setSeed() doc that might need attention:

           * Note: Although the seed value is an AtomicLong, this method
           * must still be synchronized to ensure correct semantics
           * of haveNextNextGaussian.

      Both AtomicLong and haveNextNextGaussian are implementation specific. This sounds more like an implementation note and should be removed/fixed from the specification.

            martin Martin Buchholz
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: