JDK-6470700 : Math.random() / Math.initRNG() uses "double checked locking"
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2006-09-14
  • Updated: 2019-01-24
  • Resolved: 2013-08-23
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 8
8 b106Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
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."
19-09-2006