Cleaner
public
final
class
Cleaner
extends Object
java.lang.Object | |
↳ | java.lang.ref.Cleaner |
Cleaner
manages a set of object references and corresponding cleaning actions.
Cleaning actions are registered
to run after the cleaner is notified that the object has become
phantom reachable.
The cleaner uses PhantomReference
and ReferenceQueue
to be
notified when the reachability
changes.
Each cleaner operates independently, managing the pending cleaning actions
and handling threading and termination when the cleaner is no longer in use.
Registering an object reference and corresponding cleaning action returns
a Cleanable
. The most efficient use is to explicitly invoke
the clean
method when the object is closed or
no longer needed.
The cleaning action is a Runnable
to be invoked at most once when
the object has become phantom reachable unless it has already been explicitly cleaned.
Note that the cleaning action must not refer to the object being registered.
If so, the object will not become phantom reachable and the cleaning action
will not be invoked automatically.
The execution of the cleaning action is performed by a thread associated with the cleaner. The thread runs until all registered cleaning actions have completed and the cleaner itself is reclaimed by the garbage collector.
The behavior of cleaners during System.exit
is implementation specific. No guarantees are made relating
to whether cleaning actions are invoked or not.
Unless otherwise noted, passing a null
argument to a constructor or
method in this class will cause a
NullPointerException
to be thrown.
Summary
Nested classes | |
---|---|
interface |
Cleaner.Cleanable
|
Public methods | |
---|---|
static
Cleaner
|
create(ThreadFactory threadFactory)
Returns a new |
static
Cleaner
|
create()
Returns a new |
Cleaner.Cleanable
|
register(Object obj, Runnable action)
Registers an object and a cleaning action to run when the object becomes phantom reachable. |
Inherited methods | |
---|---|
Public methods
create
public static Cleaner create (ThreadFactory threadFactory)
Returns a new Cleaner
using a Thread
from the ThreadFactory
.
A thread from the thread factory's newThread
method is set to be a daemon thread
and started to process phantom reachable objects and invoke cleaning actions.
On each call the thread factory
must provide a Thread that is suitable for performing the cleaning actions.
The cleaner terminates when it is phantom reachable and all of the registered cleaning actions are complete.
Parameters | |
---|---|
threadFactory |
ThreadFactory : a ThreadFactory to return a new Thread
to process cleaning actions |
Returns | |
---|---|
Cleaner |
a new Cleaner |
Throws | |
---|---|
IllegalThreadStateException |
if the thread from the thread
factory was not a new thread . |
SecurityException |
if the current thread is not allowed to create or start the thread. |
create
public static Cleaner create ()
Returns a new Cleaner
.
The cleaner creates a daemon thread
to process the phantom reachable objects and to invoke cleaning actions.
The context class loader
of the thread is set to the
system class loader
.
The thread has no permissions, enforced only if a
SecurityManager is set
.
All exceptions thrown by the cleaning actions in this thread are ignored.
The cleaner terminates when it is phantom reachable and all of the registered cleaning actions are complete.
Returns | |
---|---|
Cleaner |
a new Cleaner |
Throws | |
---|---|
SecurityException |
if the current thread is not allowed to create or start the thread. |
register
public Cleaner.Cleanable register (Object obj, Runnable action)
Registers an object and a cleaning action to run when the object becomes phantom reachable. Refer to the API Note above for cautions about the behavior of cleaning actions.
Parameters | |
---|---|
obj |
Object : the object to monitor |
action |
Runnable : a Runnable to invoke when the object becomes phantom reachable |
Returns | |
---|---|
Cleaner.Cleanable |
a Cleanable instance |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
TMZ Celebrity News – Breaking Stories, Videos & Gossip
Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.
Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.
🎥 Watch TMZ Live
TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.
- Exclusive TMZ interviews and court updates
- Viral celebrity videos and candid photos
- Trending gossip and entertainment news