Removing a layer of synchronization on Math.random

Eamonn McManus Eamonn.McManus at Sun.COM
Sat Jun 5 14:00:15 UTC 2010


It seems to me that if two threads call this Math.random() at the same 
time then two instances of Random() can be constructed. That contradicts 
the specification of the method, and is theoretically observable because 
the two values from Math.random() will typically not be a pair of values 
that could have been returned from consecutive calls to nextDouble() on 
a single Random instance. Granted, the chances of this actually 
mattering are infinitesimal, but I think the performance gain of 
avoiding a synchronized method only on the very first call to 
Math.random() is infinitesimal too. So I'd be inclined to leave well 
enough alone here.
Eamonn

Martin Buchholz wrote:
> Here's an optimization for Math.random() that appears to be safe,
> despite the double-check access:
>
> http://cr.openjdk.java.net/~martin/webrevs/openjdk7/Math.random/
>
> David, if you agree, could you file a bug?
>
> Thanks,
>
> Martin
>   



More information about the core-libs-dev mailing list