[concurrency-interest] ThreadLocalRandom initial seed

Mark Thornton mthornton at optrak.com
Fri Jun 3 09:11:37 EDT 2011


On 03/06/11 12:38, David Holmes wrote:
> Thanks for correcxting me on that. This sure is confusing. It would 
> seem that 6955840 although trying to allow setSeed to be called from 
> the random constructor was completely pointless because Random stopped 
> calling the setSeed method.
> I hope Martin is watching and can give some input here. It certainly 
> seems that we never properly initialize a ThreadLocalRandom!
> David
>
>
>          ThreadLocalRandom() {
>              super(0); // don't bother generating a seed we don't use
>     	setSeed(System.nanoTime());
>              initialized = true;
>          }
>
>     Mark Thornton
>

For people with very fast machines (and/or poor implementations of 
System.nanoTime()), the following initialisation may be better:

     private static final Random seedSource = new Random();

     ThreadLocalRandom() {
         super(0); // don't bother generating a seed we don't use
	setSeed(seedSource.nextLong());
         initialized = true;
     }


Mark

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20110603/9ed53e75/attachment.html>


More information about the Concurrency-interest mailing list