JDK-4924316 : java.util.Random spec problem on synchronization
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2003-09-17
  • Updated: 2005-11-12
  • Resolved: 2005-11-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b61Fixed
Related Reports
Relates :  
Description
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.

Comments
EVALUATION java.util.Random spec api spec clarifications needed. ###@###.### 2003-09-21
21-09-2003