United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6470700 Math.random() / Math.initRNG() uses "double checked locking"
JDK-6470700 : Math.random() / Math.initRNG() uses "double checked locking"

Details
Type:
Bug
Submit Date:
2006-09-14
Status:
Closed
Updated Date:
2014-01-06
Project Name:
JDK
Resolved Date:
2013-08-23
Component:
core-libs
OS:
linux
Sub-Component:
java.lang
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Duplicate:
Duplicate:

Sub Tasks

Description
FULL PRODUCT VERSION :
java version "1.6.0-rc"
Java(TM) SE Runtime Environment (build 1.6.0-rc-b95)
Java HotSpot(TM) Server VM (build 1.6.0-rc-b95, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux xxxxxx 2.6.16 #2 SMP Mon Mar 27 17:30:45 CEST 2006 x86_64 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
Math.random() and Math.initRNG() uses DCL (double checked locking) on " private static Random randomNumberGenerator".

Breaks in a multi processor environment, resulting in a NPE.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Init field "private static Random randomNumberGenerator" in static initializer

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Spurius NullPointerException

REPRODUCIBILITY :
This bug can be reproduced rarely.

CUSTOMER SUBMITTED WORKAROUND :
Do not use Math.random

                                    

Comments
EVALUATION

The standard technique is to have a nested subclass with a final field
initialized on demand (lazily)

private static class NestedClass { 
  public final static Random randomNumberGenerator = ...
}

and then refer to NestedClass.randomNumberGenerator

However, looking at the implementation, the fact that
initialized fields are always visible in the initializing thread,
together with the fact that Random is synchronized, suggests to me
that NPE is impossible, or at least should never be observable with
the Sun implementation.  As usual, "Show me a small failing test case."
                                     
2006-09-19
URL:   http://hg.openjdk.java.net/jdk8/tl/jdk/rev/223be1d3494f
User:  chegar
Date:  2013-08-23 09:03:33 +0000

                                     
2013-08-23
URL:   http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/223be1d3494f
User:  lana
Date:  2013-08-31 01:29:26 +0000

                                     
2013-08-31



Hardware and Software, Engineered to Work Together