JavaMemoryModel: Re: SoftReference not thread-safe ?

From: Hanson Char (hanson.char@gmail.com)
Date: Wed Jan 19 2005 - 15:39:44 EST


I refer to the latest jdk1.5. Just wonder what's this forum thought on the
SoftReferece.get() method:

  public T get() {
      T o = super.get();
      if (o != null) this.timestamp = clock;
      return o;
  }

which updates the timestamp if necessary, but not being thread-safe in
that the timiestamp is of type long but there is no synchronization.

So in theory if multiple threads get hold of the same SoftReference
and invoke the get method,

1) Can the timestamp result in a "corrupted" state ? Or,

2) Would all the thread then have the same clock value in their
working copies, and therefore will always update the timestamp with
that clock value ?

What's the worst case scenario when this happened ? Should one bother
to synchronize on the SoftReference instance when this method is
invoked ?

H
-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:01:09 EDT