struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Speed <psp...@progeeks.com>
Subject Re: [OT] Re: WeakReference
Date Thu, 02 Dec 2004 10:14:46 GMT
Ok, I haven't run this, but let me try to interpret what I see inline...

Dakota Jack wrote:

> Thanks for the response, Paul.  Here's what I am up to.  I can get an
> object from the weak reference created from a strong reference.  What
> I want to do, and am not sure if I can (I am starting to think I
> cannot), is to grab the object with the weak reference and make
> changes which will happen also with the strong reference.  So far, it
> seems that when I have the object of a weak reference created form a
> strong reference and change this object, it does not affect the object
> of the strong reference.
> 
> E.g.
> 
> package com.crackwillow.deploy;
> 
> import java.lang.ref.WeakReference;
> import java.lang.ref.Reference;
> 
> public class MyReference {
>   public static void main(String [] params) {
>     String        string = new String("Aaaaaaaaaa");

Created a string containing "Aaaaaaa".

>     WeakReference wr     = new WeakReference(string);

Created a weak reference to said string "Aaaaaa".

>     wr = replace(wr);

Created a second weak reference to a different string "Baaaaa".

>     System.out.println(string);

Print "Aaaaaa".

>     System.out.println(wr);

Print the second weak reference

>     string = (String)((Reference)wr).get();

Set string to be the second created string "Baaaaa".

>     System.out.println(string);

Print "Baaaaa".

At least that's what I'd expect it to do.

>   }
> 
>   public static WeakReference replace(WeakReference wr) {
>     return new WeakReference(((String)wr.get()).replaceAll("A","B"));
>   }
> }
> 
> Apparently there is no connection between the objects referred to by
> string and wr in the code shown above.

Well, sure.  Because you've replaced your old wr reference with a 
completely new one containing a completely different String reference.

> 
> Is this clearer?

Some things are definitely clearer. ;)
-Paul

> 
> Jack
> 
> 
> On Thu, 02 Dec 2004 02:03:39 -0500, Paul Speed <pspeed@progeeks.com> wrote:
> 
>>A WeakReference is just a way of holding a reference to an object that
>>will not keep it from being garbage collected (a very useful thing).
>>There are also ways that you can track when it has been garbage
>>collected.  Once it has been garbage collected, it's gone though.  All
>>you have is any data you originally associated with your WeakReference
>>(by subclassing or some other method).  You cannot access the referenced
>>object anymore because it doesn't exist.
>>
>>While the object still exists (ie: is strongly referenced some where),
>>you can still access it through the WeakReference.  You just have to
>>expect that at some point you may go to retrieve it from the
>>WeakReference and get a null.
>>
>>Maybe it would help to know what you are trying to do with it.  There
>>are other Reference implementations that may be better suited.
>>
>>-Paul
>>
>>Dakota Jack wrote:
>>
>>
>>>Working here on a "new" paradigm, sort of, I think.
>>>
>>>Apparently you can track what has happened to a strong reference with
>>>a weak reference but you cannot manipulate the object referred to by
>>>the strong reference by manipulating the weak reference.  Is that
>>>right?  Seems odd to me.
>>>
>>>Jack
>>>
>>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
>>For additional commands, e-mail: dev-help@struts.apache.org
>>
>>
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Mime
View raw message