----- Original Message ----- 
From: "Niclas Hedhman" <[EMAIL PROTECTED]>
To: "Avalon framework users" <[EMAIL PROTECTED]>
Sent: Monday, December 15, 2003 11:28 AM
Subject: Re: When use ServiceManager.release(obj)?


> On Tuesday 16 December 2003 01:46, Jonathan Hawkes wrote:
> > No, actually, I meant it as I said it.  Consider the following:
> >
> > class A {
> > B b;
> > public String toString() {
> >     return "I am an A";
> > }
> > protected void finalize() {
> >     System.out.println(b);  // Just do anything with b
> >     // b is reachable here
> > }
> > }
> >
> > class B {
> > A a;
> > public String toString() {
> >     return "I am a B";
> > }
> > protected void finalize() {
> >     System.out.println(a); // Just do anything with a
> >     // a is reachable here
> > }
> > }
> >
> > public class Test {
> >     public static void main(String[] args) {
> >         Test test = new Test();
> >         test.setup();
> >         // wait for GC
> >         while (true) {
> >             System.gc();
> >             Thread.sleep(100);
> >         }
> >     }
> >     private void setup() {
> >         A a = new A();
> >         B b = new B();
> >         a.b = b;
> >         b.a = a;
> >     }
> > }
>
> I don't know what you expect me to experience, and since the JLS has no
answer
> to what will happen in detail, I had to "observe behaviour" which is
> per-definition wrong.

Please explain.

>
> As it is written now, GC will NOT happen at all (at least not on my JVM).
> Because there is no low memory condition, and GC will hold its horses.
> Add a System.gc() at // wait for GC, and I get B before A (irrelevant, the
> spec says either one will have the finalize() called first, or at the same
> time on a multiprocessor system).
> I am a B
> I am an A

Sorry, my example was meant to be purely pseudo-code.  I didn't even compile
it.
But you are right, I have modified the garbage collection loop (see above).

However, your test worked exactly as I predicted.  You're right, it doesn't
matter (and it's not defined) which comes first "I am a B" or "I am an A".
What I was simply pointing out is that they will both happen.  A.b is
reachable from the A.finalize() method.  B.a is reachable from the
B.finalization() method.  This MUST be this way.  Anything else would just
be absurd and allow dangling references (which Java does not).  Both the A
instance and the B instance will have finalize() invoked before they are
discarded.

>
> What you said was (or as least as I interpret any ambiguity). A reference
B,
> then A don't reference B and A's finalize() will be called, which is not
> true. B's finalize() is called.

Ok, forgive me; but I can't make heads or tails of what you are saying here.

>
> Your quoted text bears little weight compared to the Execution chapter in
the
> JLS. It is very imprecise and doesn't even use the proper terminology for
> reachable/finalizer-reachable/unreachable (text below talks about "can be
> accessed" )

Well, complain to Sun.  I didn't write it myself.  It is directly from their
quoted API docs.  True, they may not be as technically specific as the JLS,
but a hell of a lot easier to understand.

>
> Sorry, don't carry an argument with inadequate evidence... ;o)
>
> Niclas

There is no reason to be a prick.  And since when are the API docs
inadequate evidence?


> >
> > The general contract of finalize is that it is invoked if and when the
> > JavaTM virtual machine has determined that there is no longer any means
by
> > which this object can be accessed by any thread that has not yet died,
> > <bold>except as a result of an action taken by the finalization of some
> > other object or class which is ready to be finalized.</bold>
> >
> > After the finalize method has been invoked for an object, <bold>no
further
> > action is taken until the Java virtual machine has again determined that
> > there is no longer any means by which this object can be accessed by any
> > thread that has not yet died, <bolder>including possible actions by
other
> > objects or classes which are ready to be finalized, at which point the
> > object may be discarded. </bolder></bold>
> >
> >
> > INCLUDING POSSIBLE ACTIONS BY OTHER OBJECTS OR CLASSES WHICH ARE READY
TO
> > BE FINALIZED
> >
> >
> > ----- Original Message -----
> > From: "Niclas Hedhman" <[EMAIL PROTECTED]>
> > To: "Avalon framework users" <[EMAIL PROTECTED]>
> > Sent: Monday, December 15, 2003 9:57 AM
> > Subject: Re: When use ServiceManager.release(obj)?
> >
> > > On Monday 15 December 2003 23:21, Jonathan Hawkes wrote:
> > > > Before an object is discarded, any object that references that
object
> >
> > will
> >
> > > > have its finalize() method invoked.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to