Re: SoftReference operation synchronization detail




"Stanimir Stamenkov" <stanio@xxxxxx> wrote in message news:1149088649.996756.239730@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
The following may have more apparent effect when using a WeakReference.
Suppose I initialize a class member field which is a SoftReference, at
some point:

SoftReference cache = new SoftReference(obj);

Then I have the following code accessing the referenced object, in a
method body:

Object obj = null;
if (cache != null) {
obj = cache.get();
}
if (obj == null) {
obj = new Object(); /* re-initialize obj */
cache = new SoftReference(obj);
}
/* do with obj */

Is it guaranteed the 'cache' won't be cleared after the 'if (cache !=
null)' evaluates to true and prior executing the first statement in the
'if' block?

Is it guaranteed 'Reference.get()' will return a non-null result if the
corresponding SoftReference or WeakReference is not cleared yet?, so I
could go without the explicit 'obj = null' initialization in the above
example:

Object obj;
if (cache != null) {
obj = cache.get();
} else {
obj = new Object(); /* re-initialize obj */
cache = new SoftReference(obj);
}
/* do with obj */

I don't understand your exact question, but the information I think you want is that second code is unsafe. cache.get() might return null. The first code example is better. Note that the reference called "cache" won't magically turn into null behind your back. It's only the referent within the SoftReference which may become null (which is why get() sometimes returns null). So depending on the design of the rest of your code, the check for cache != null might be unnescessary.

- Oliver

.



Relevant Pages

  • SoftReference operation synchronization detail
    ... Suppose I initialize a class member field which is a SoftReference, ... SoftReference cache = new SoftReference; ... Object obj = null; ...
    (comp.lang.java.programmer)
  • Re: SoftReference operation synchronization detail
    ... Suppose I initialize a class member field which is a SoftReference, ... SoftReference cache = new SoftReference; ... Object obj = null; ...
    (comp.lang.java.programmer)
  • Re: SoftReference operation synchronization detail
    ... SoftReference which may become null sometimes returns ... thought) while playing with those Reference classes - I've got 'null' ... SoftReference cache = new SoftReference); ... So the garbage collector may decide it wants to collect that Foo object. ...
    (comp.lang.java.programmer)
  • Re: Using SoftReferences for caching
    ... You need to associate each SoftReference with a ReferenceQueue object. ... > I'm writing an application where I have a memory cache of objects ... > object can be as large as the heap. ...
    (comp.lang.java.programmer)
  • Re: Effizientes Caching von Key-Value-Paaren
    ... vorausgesetzt dein Code baut nicht auf eine der im Javadoc zu WeakHashMap erwähnten Invarianten normaler Maps auf. ... Es gibt plugabble Cache Lösungen die das vereinfachen. ... Allerdings ist es auch nicht schwer so etwas selber zu bauen, dann mit SoftReference und evtl. ...
    (de.comp.lang.java)