[concurrency-interest] ThreadLocalRandom initial seed

Aleksey Shipilev aleksey.shipilev at gmail.com
Fri Jun 3 04:12:47 EDT 2011


David,

I had checked out jsr166 source, instrumented TLR, and this is the output:

TLR.<init>(): rnd = 0
TLR.next(): rnd before = 0
TLR.next(): rnd after = 11
0
TLR.next(): rnd before = 11
TLR.next(): rnd after = 277363943098
6118
TLR.next(): rnd before = 277363943098
TLR.next(): rnd after = 11718085204285
1895
TLR.next(): rnd before = 11718085204285
TLR.next(): rnd after = 49720483695876
7186
TLR.next(): rnd before = 49720483695876
TLR.next(): rnd after = 102626409374399
7366

So, rnd (TLR's "seed") is always 0, and setSeed() is not being called
from super-Random. Forcibly setting rnd = System.nanoTime() in
TLR.<init>() helps.

-Aleksey.
On Fri, Jun 3, 2011 at 10:43 AM, David Holmes <davidcholmes at aapt.net.au> wrote:
> Aleksey,
>
> The problem is not the seed - all threads do get a distinct seed. The problem is that the first call to nextInt(n) is always returning zero - hence the sequence will always be the same. I haven't yet determined exactly what is going wrong.
>
> David
>
>> -----Original Message-----
>> From: Aleksey Shipilev [mailto:aleksey.shipilev at gmail.com]
>> Sent: Friday, 3 June 2011 3:23 PM
>> To: dholmes at ieee.org
>> Cc: Kasper Nielsen; concurrency-interest at cs.oswego.edu
>> Subject: Re: [concurrency-interest] ThreadLocalRandom initial seed
>>
>>
>> Hi David, Kasper,
>>
>> This issue is still reproduced on jdk7b144, as well as
>> bootclasspath'ed today's binary bundle of jsr166.jar. I guess it still
>> not fixed then.
>>
>> -Aleksey.
>>
>> On Fri, Jun 3, 2011 at 2:09 AM, David Holmes
>> <davidcholmes at aapt.net.au> wrote:
>> > I thought this had been addressed by 6955840
>> >
>> > http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6955840
>> >
>> > "ThreadLocalRandom bug - overridden setSeed(long) method is not
>> invoked for
>> > java.util.Random(long)"
>> >
>> > David
>> >
>> >> -----Original Message-----
>> >> From: concurrency-interest-bounces at cs.oswego.edu
>> >> [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Kasper
>> >> Nielsen
>> >> Sent: Friday, 3 June 2011 7:44 AM
>> >> To: concurrency-interest at cs.oswego.edu
>> >> Subject: Re: [concurrency-interest] ThreadLocalRandom initial seed
>> >>
>> >>
>> >> Good catch,
>> >>
>> >> that is definitely a bug.
>> >>
>> >> Looks like the fix to
>> >> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6937857
>> >> introduced a non-compatible change by not calling setSeed() in
>> >> Random(long seed) anymore.
>> >>
>> >> Cheers
>> >>    Kasper
>> >>
>> >> On 02-06-2011 16:29, Aleksey Shipilev wrote:
>> >> > Hi,
>> >> >
>> >> > I've been stumbled upon ThreadLocalRandom seed behavior. JavaDoc
>> >> > reads: "ThreadLocalRandom is initialized with an internally generated
>> >> > seed that may not otherwise be modified." I would expect TLR
>> called in
>> >> > several threads concurrently to have different global values. But
>> >> > apparently the internal seed in TLR always has the same seed
>> (which is
>> >> > default value for long).
>> >> >
>> >> > Is this intentional? Or just oversight that should be fixed?
>> >> >
>> >> > I.e. I would expect my test [1] print all-different values
>> per thread,
>> >> > like regular Random does.
>> >> >
>> >> > That's what happens now:
>> >> >
>> >> > Regular thread-local Random
>> >> > 780
>> >> > 4307
>> >> > 9112
>> >> > 4368
>> >> > 6673
>> >> > ========
>> >> > 4143
>> >> > 4905
>> >> > 2331
>> >> > 154
>> >> > 2887
>> >> > ========
>> >> > 9586
>> >> > 6161
>> >> > 9948
>> >> > 4179
>> >> > 3608
>> >> > ========
>> >> >
>> >> > ThreadLocalRandom
>> >> > 0
>> >> > 6118
>> >> > 1895
>> >> > 7186
>> >> > 7366
>> >> > ========
>> >> > 0
>> >> > 6118
>> >> > 1895
>> >> > 7186
>> >> > 7366
>> >> > ========
>> >> > 0
>> >> > 6118
>> >> > 1895
>> >> > 7186
>> >> > 7366
>> >> > ========
>> >> >
>> >> > Thanks,
>> >> > Aleksey.
>> >> >
>> >> > [1]
>> >> > import java.util.Random;
>> >> > import java.util.concurrent.ThreadLocalRandom;
>> >> >
>> >> > public class Main {
>> >> >
>> >> >      public static ThreadLocal<Random>  random = new
>> >> ThreadLocal<Random>() {
>> >> >          @Override
>> >> >          protected Random initialValue() {
>> >> >              return new Random();
>> >> >          }
>> >> >      };
>> >> >
>> >> >      public static void main(String[] args) throws
>> >> InterruptedException {
>> >> >
>> >> >          System.out.println("Regular thread-local Random");
>> >> >          for (int i = 0; i<  3; i++) {
>> >> >              Thread t = new Thread(new Runnable() {
>> >> >                  @Override
>> >> >                  public void run() {
>> >> >                      for (int c = 0; c<  5; c++) {
>> >> >
>> >> System.out.println(random.get().nextInt(10000));
>> >> >                      }
>> >> >                      System.out.println("========");
>> >> >                  }
>> >> >              });
>> >> >              t.start();
>> >> >              t.join();
>> >> >          }
>> >> >
>> >> >          System.out.println("ThreadLocalRandom");
>> >> >          for (int i = 0; i<  5; i++) {
>> >> >              Thread t = new Thread(new Runnable() {
>> >> >                  @Override
>> >> >                  public void run() {
>> >> >                      for (int c = 0; c<  5; c++) {
>> >> >
>> >> > System.out.println(ThreadLocalRandom.current().nextLong(10000));
>> >> >                      }
>> >> >                      System.out.println("========");
>> >> >                  }
>> >> >              });
>> >> >              t.start();
>> >> >              t.join();
>> >> >          }
>> >> >
>> >> >      }
>> >> >
>> >> > }
>> >> > _______________________________________________
>> >> > Concurrency-interest mailing list
>> >> > Concurrency-interest at cs.oswego.edu
>> >> > http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>> >> >
>> >>
>> >> _______________________________________________
>> >> Concurrency-interest mailing list
>> >> Concurrency-interest at cs.oswego.edu
>> >> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>> >>
>> >
>> > _______________________________________________
>> > 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