Right, which means we should also add a readResolve() method to return
ThreadLocalRandom.current(). All you need to do is add this to the
class and
the format should be compatible with Java 1.7. This means that if
someone made
the mistake of writing ThreadLocalRandom instances, they would now
still be able
to read, but would instead get the correct instance back for their
thread:
/**
* We keep the same serial persistent format as in Java 1.7.
*/
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("rnd", long.class),
new ObjectStreamField("initialized", boolean.class),
new ObjectStreamField("pad0", long.class),
new ObjectStreamField("pad1", long.class),
new ObjectStreamField("pad2", long.class),
new ObjectStreamField("pad3", long.class),
new ObjectStreamField("pad4", long.class),
new ObjectStreamField("pad5", long.class),
new ObjectStreamField("pad6", long.class),
new ObjectStreamField("pad7", long.class),
};
/**
* Writes the ThreadLocalRandom object to the ObjectOutputStream
using the
* same format as in the past.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
ObjectOutputStream.PutField fields = out.putFields();
fields.put("rnd", rnd);
fields.put("initialized", true);
fields.put("pad0", 0L);
fields.put("pad1", 0L);
fields.put("pad2", 0L);
fields.put("pad3", 0L);
fields.put("pad4", 0L);
fields.put("pad5", 0L);
fields.put("pad6", 0L);
fields.put("pad7", 0L);
out.writeFields();
}
/**
* Reads the ThreadLocalRandom object from the stream in order to
keep
* the format compatible, but does not use any of the read data.
*/
private void readObject(ObjectInputStream in)
throws ClassNotFoundException, IOException {
in.readFields();
// we can ignore the values, since we will replace them in the
// readResolve() method anyway
}
/**
* Once the ThreadLocalRandom object has been read from the
stream, we
* throw it away and instead return the correct instance for our
thread.
*/
private Object readResolve() {
return current();
}