12.6.2
Finalizer Invocations are Not Ordered
EXECUTION
A finalizable object cannot also be unreachable; it can be reached because its
finalizer may eventually be invoked, whereupon the thread running the finalizer
will have access to the object, as
this
( 15.7.2). Thus, there are actually only
eight possible states for an object.
After an object has been finalized, no further action is taken until the auto
matic storage management determines that it is unreachable. Because of the way
that an object progresses from the
unfinalized
state through the
finalizable
state to
the
finalized
state, the
finalize
method is never automatically invoked more than
once by a Java Virtual Machine for each object, even if the object is again made
reachable after it has been finalized.
Explicit invocation of a finalizer ignores the current state of the object and
does not change the state of the object from unfinalized or finalizable to finalized.
If a class does not override method
finalize
of class
Object
(or overrides it
in only a trivial way, as described above), then if instances of such as class
become unreachable, they may be discarded immediately rather than made to
await a second determination that they have become unreachable. This strategy is
indicated by the dashed arrow (
O
) in the transition diagram.
Java programmers should also be aware that a finalizer can be automatically
invoked, even though it is reachable, during finalization on exit ( 12.9); more
over, a finalizer can also be invoked explicitly as an ordinary method. Therefore,
we recommend that the design of
finalize
methods be kept simple and that they
be programmed defensively, so that they will work in all cases.
12.6.2 Finalizer Invocations are Not Ordered
Java imposes no ordering on finalize method calls. Finalizers may be called in any
order, or even concurrently.
As an example, if a circularly linked group of unfinalized objects becomes
unreachable (or finalizer reachable), then all the objects may become finalizable
together. Eventually, the finalizers for these objects may be invoked, in any order,
or even concurrently using multiple threads. If the automatic storage manager
later finds that the objects are unreachable, then their storage can be reclaimed.
It is straightforward to implement a Java class that will cause a set of finalizer
like methods to be invoked in a specified order for a set of objects when all the
objects become unreachable. Defining such a class is left as an exercise for the
reader.
234
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved