src/java.base/share/classes/java/util/concurrent/ThreadLocalRandom.java

Print this page
rev 12972 : 8140606: Update library code to use internal Unsafe
Reviewed-by: duke


1033      * @throws java.io.IOException if an I/O error occurs
1034      */
1035     private void writeObject(java.io.ObjectOutputStream s)
1036         throws java.io.IOException {
1037 
1038         java.io.ObjectOutputStream.PutField fields = s.putFields();
1039         fields.put("rnd", U.getLong(Thread.currentThread(), SEED));
1040         fields.put("initialized", true);
1041         s.writeFields();
1042     }
1043 
1044     /**
1045      * Returns the {@link #current() current} thread's {@code ThreadLocalRandom}.
1046      * @return the {@link #current() current} thread's {@code ThreadLocalRandom}
1047      */
1048     private Object readResolve() {
1049         return current();
1050     }
1051 
1052     // Unsafe mechanics
1053     private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
1054     private static final long SEED;
1055     private static final long PROBE;
1056     private static final long SECONDARY;
1057     static {
1058         try {
1059             SEED = U.objectFieldOffset
1060                 (Thread.class.getDeclaredField("threadLocalRandomSeed"));
1061             PROBE = U.objectFieldOffset
1062                 (Thread.class.getDeclaredField("threadLocalRandomProbe"));
1063             SECONDARY = U.objectFieldOffset
1064                 (Thread.class.getDeclaredField("threadLocalRandomSecondarySeed"));
1065         } catch (ReflectiveOperationException e) {
1066             throw new Error(e);
1067         }
1068     }
1069 }


1033      * @throws java.io.IOException if an I/O error occurs
1034      */
1035     private void writeObject(java.io.ObjectOutputStream s)
1036         throws java.io.IOException {
1037 
1038         java.io.ObjectOutputStream.PutField fields = s.putFields();
1039         fields.put("rnd", U.getLong(Thread.currentThread(), SEED));
1040         fields.put("initialized", true);
1041         s.writeFields();
1042     }
1043 
1044     /**
1045      * Returns the {@link #current() current} thread's {@code ThreadLocalRandom}.
1046      * @return the {@link #current() current} thread's {@code ThreadLocalRandom}
1047      */
1048     private Object readResolve() {
1049         return current();
1050     }
1051 
1052     // Unsafe mechanics
1053     private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
1054     private static final long SEED;
1055     private static final long PROBE;
1056     private static final long SECONDARY;
1057     static {
1058         try {
1059             SEED = U.objectFieldOffset
1060                 (Thread.class.getDeclaredField("threadLocalRandomSeed"));
1061             PROBE = U.objectFieldOffset
1062                 (Thread.class.getDeclaredField("threadLocalRandomProbe"));
1063             SECONDARY = U.objectFieldOffset
1064                 (Thread.class.getDeclaredField("threadLocalRandomSecondarySeed"));
1065         } catch (ReflectiveOperationException e) {
1066             throw new Error(e);
1067         }
1068     }
1069 }