[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [cp-patches] [PATCH] Fix PR classpath/24086, PR classpath/24091, PR

From: Boehm, Hans
Subject: RE: [cp-patches] [PATCH] Fix PR classpath/24086, PR classpath/24091, PR classpath/24104 et al. ...
Date: Thu, 13 Oct 2005 14:28:34 -0700

> -----Original Message-----
> From:  Jeroen Frijters
> Finalizers are anything but simple.
That much we agree on.  And I agree with most of the advice in your

> They really should only 
> be used to free native resources (as a last resort), not for 
> any type of Java clean up. Blocking and locking should be 
> avoided in a finalizer. Typically there is only one finalizer 
> thread and you don't want to hog that by blocking on I/O.
Blocking on IO indeed seems dubious.  But finalizers almost always need
to acquire at least one lock.  In the example in your blog, the finalize
method should be synchronized (or start with synchronized(this) {} ) to
guarantee reachability of the object while one of the other synchronized
methods is running.

If you follow the (admittedly baroque) rules, there are safe ways to
clean up Java resources with finalizers as well, though clearly not when
timing matters.

And you actually need to follow very similar rules for java.lang.ref.

For details see my JavaOne talk.  (A version of the slides, with a
different set of
typos and logos, can also be found at
f .)


> I wrote about how finalize should be used on my blog: 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]