Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6379897

Random is not thread-safe; seed should be final

    • b73
    • generic
    • generic

      Random has its state in an AtomicLong, which should be final so that
      no threads see it as uninitialized.

      Doug Lea writes:

      David Holmes wrote:

      >> Hi Doug,
      >>
      >> This is a link to the java.net forums for mustang.
      >>
      >> http://forums.java.net/jive/thread.jspa?threadID=2443&tstart=0
      >>

      Yup. The seed field ought to be declared final.
      I bet the reason that it wasn't (when the internal sun.AtomicLong was used, in
      1.4) was the need to set in readObject. Which had no solution
      until 1.5, but no one noticed (It probably should have been us when
      converting to use j.u.c.a.AtomicLong).
      Coping requires an ugly unsafe.putObjectVolatile.

            martin Martin Buchholz
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: