Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

HardReference?

Thread view: 
Hendrik Maryns - 09 Jun 2006 01:34 GMT
Hello all,

I am hacking around in Jakarta Commons Collections’s ReferenceMap.  I am
trying to make it generic, for the fun of it.  Now the trick they use is
to check whether either value or key have to be wrapped in a soft or
weak reference each time, and then do it when necessary.  On the other
hand, each time a key or value is asked, if they have to be wrapped,
they are first cast to Reference, and then the object is got out.  This
works, because the AbstractHashedMap it inherits from, is non-generic
and stores Objects.  But I generified AbstractHashedMap, and now it
refuses to store the Reference<K> objects in the key slot (because it
expects K), and similar with V for values.  So the easy solution would
be to have a HardReference, which does nothing but inherit from
Reference and wrap the object it refers to, in a hard way.
Unfortunately, Reference says it should not be subclassed.  Would it be
acceptable to subclass Reference for this purpose?  What is the danger
of it?

Or does anyone see a different solution?

TIA, H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Piotr Kobzda - 09 Jun 2006 08:57 GMT
> Unfortunately, Reference says it should not be subclassed.  Would it be
> acceptable to subclass Reference for this purpose?  What is the danger
> of it?

You can't subclass Reference directly, there are no constructors visible
outside a java.lang.ref package.
Anyway, you can use something like the following example for your purposes.

<code>
import java.lang.ref.SoftReference;

public class HardReference<T> extends SoftReference<T> {

    @SuppressWarnings("unused") private T strongRef;

    public HardReference(T referent) {
        super(referent);
        strongRef = referent;
    }

}
</code>

Regards,
piotr
Hendrik Maryns - 09 Jun 2006 12:26 GMT
Hi Piotr,

Piotr Kobzda schreef:

>> Unfortunately, Reference says it should not be subclassed.  Would it be
>> acceptable to subclass Reference for this purpose?  What is the danger
[quoted text clipped - 18 lines]
> }
> </code>

I’m not sure I understand why this would work.  How does the garbage
collector know this is not a Soft reference it can collect, and why is
the unused T needed?

Thanks, H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Chris Uppal - 09 Jun 2006 14:00 GMT
> public class HardReference<T> extends SoftReference<T> {
>
[quoted text clipped - 6 lines]
>
> }

Probably better to extend WeakReference (or maybe even PhantomReference) to
avoid all the timestamping and other extra logic for SoftReferences.

Also I wonder if an implementation like:

=====================
public class HardReference<T>
extends WeakReference<T>     // or Phantom ?
{
   private final T strongRef;

   public // ctor
   HardReference(T referent)
   {
       super(null);        // deliberately don't pass referent to superclass
       strongRef = referent;
    }

   public T
   get()
   {
        return strongRef;
   }

   // maybe should override clear() and isEnqueued() too
}

=====================

would give the GC less work to do.  I haven't tried it, and I don't know enough
about the GC(s) implementation(s) to know -- it might break everything ;-)

   -- chris
Hendrik Maryns - 13 Jun 2006 09:27 GMT
Hi Chris,

Chris Uppal schreef:

>> public class HardReference<T> extends SoftReference<T> {
>>
[quoted text clipped - 38 lines]
> would give the GC less work to do.  I haven't tried it, and I don't know enough
> about the GC(s) implementation(s) to know -- it might break everything ;-)

Thanks for your answer, but you’re only confusing me more.  Now which of
both suggestions should I take?  And it’s still not clear to me why
these are correct, a hint on what to read about references is enough.

Thanks, H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Chris Uppal - 13 Jun 2006 10:15 GMT
> Thanks for your answer, but you're only confusing me more.  Now which of
> both suggestions should I take?

I'd definitely avoid inheriting from SoftReference -- that just adds work for
the system.  So base it on Weak or Phantom refs.  Don't bother with my other
elaborations unless you have good reason to suppose they'd help (i.e something
more concrete than just me wondering aloud /whether/ they'd help).

> And it's still not clear to me why
> these are correct, a hint on what to read about references is enough.

It's just that by adding another field to the WeakReference which /also/ points
to the object in question, we have added a strong (i.e. normal) reference
between those objects.  That is additional to the weak reference which the GC
will manage.  Since we never let go of the HardReference object (until we don't
want it any more) it will stay strongly reachable.  But then, since it has a
strong reference to the target object that will /also/ stay strongly reachable.
Thus that object will never be GCed while the HardReference itself is still in
use.

Perhaps you are being confused by the names.  A WeakReference (or Soft or
Phantom) is only weak in it's "referent" (the object referece passed to the
constructor), all other fields are ordinary Java (i.e. strong) references.

   -- chris
Hendrik Maryns - 13 Jun 2006 10:33 GMT
Chris Uppal schreef:

>> Thanks for your answer, but you're only confusing me more.  Now which of
>> both suggestions should I take?
[quoted text clipped - 19 lines]
> Phantom) is only weak in it's "referent" (the object referece passed to the
> constructor), all other fields are ordinary Java (i.e. strong) references.

Ok, many thanks.  I sort of guessed this was the explanation, but felt
too unsure.  You reassured me :-)

In the meantime, I found a better solution which doesn’t use the hard
reference at all, but hey, I have it in memory now...

Thanks again, H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.