[concurrency-interest] ThreadLocalRandom.nextSecondarySeed() re-initializes TLR's seed

Doug Lea dl at cs.oswego.edu
Thu Jun 19 08:02:06 EDT 2014


On 06/19/2014 04:48 AM, Peter Levart wrote:
> Or, even better, why not just using the next value from the "seeder" sequence
> for the initial value of "secondary" seed and avoid interaction with TLR's main
> seed/probe:

Thanks! Or better, just use mix32:

>
> +            if ((r = (int)mix64(seeder.getAndAdd(SEEDER_INCREMENT))) == 0)
=>
      if ((r = mix32(seeder.getAndAdd(SEEDER_INCREMENT))) == 0)

I committed this to jsr166 cvs. As you noted, this only addresses
an uncommon performance glitch. I don't have any further ideas
since we discussed last year the tradeoffs between computing
decent quality initial seeds versus class-loading.
I still think we have the best practical compromise in place.

-Doug

>           }
>           UNSAFE.putInt(t, SECONDARY, r);
>
>
> Regards, Peter
>
> On 06/19/2014 10:37 AM, Peter Levart wrote:
>> Hi,
>>
>> I noticed an inconsistency in calling TLR.localInit() method. Everywhere it's
>> called conditionaly if thread-local "probe" is zero except in
>> TLR.nextSecondarySeed() where it's called if "secondary" seed is zero. This
>> re-initializes the "probe" and "seed" even though they might have already been
>> initialized. It's not a big deal, because this happens at most once per
>> thread, but it would be more consistent to call localInit() conditionaly, I
>> think:
>>
>>
>> diff -r 5b45a5efe417
>> src/share/classes/java/util/concurrent/ThreadLocalRandom.java
>> --- a/src/share/classes/java/util/concurrent/ThreadLocalRandom.java Tue May 20
>> 10:11:23 2014 +0400
>> +++ b/src/share/classes/java/util/concurrent/ThreadLocalRandom.java Thu Jun 19
>> 10:34:18 2014 +0200
>> @@ -1034,7 +1034,8 @@
>>              r ^= r << 5;
>> }
>>          else {
>> - localInit();
>> +            if (UNSAFE.getInt(t, PROBE) == 0)
>> + localInit();
>>              if ((r = (int)UNSAFE.getLong(t, SEED)) == 0)
>>                  r = 1; // avoid zero
>>          }
>>
>>
>>
>> Regards, Peter
>>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>



More information about the Concurrency-interest mailing list