Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Cleaner Class

Java Cleaner Class

java.lang.ref.Cleaner is a utility introduced in Java 9 for managing object cleanup, providing a safe way to clean up resources during garbage collection. It can replace finalize method, releasing external resources by performing cleanup operations when the object is no longer in use, and it is more efficient and reliable than finalize is more efficient and reliable.

1. Cleaner The Role of

Cleaner allows us to register a cleanup action that is automatically executed when the object becomes unreachable (i.e., about to be garbage collected). Typical application scenarios include:

2. Cleaner Basic usage of

The following are Cleaner The basic usage of

3. Usage Example

The following example shows how to use Cleaner to create a resource that needs cleanup and automatically perform the cleanup operation when the object becomes unreachable:

Example

import

java.lang.ref.Cleaner

;

public

class

CleanerExample

{

   

// Create a Cleaner instance

   

private

static

final

Cleaner cleaner

=

Cleaner.

create

(

)

;

   

// Define a resource that needs cleanup

   

static

class

Resource

implements

AutoCloseable

{

       

// Cleanup task - used to release resources

       

private

final

Cleaner.

Cleanable

cleanable

;

        Resource

(

)

{

           

// Register the cleanup task, executed when the Resource instance becomes unreachable

           

this

.

cleanable

=

cleaner.

register

(

this

,

new

ResourceCleaner

(

)

)

;

           

System

.

out

.

println

(

"Resource initialized"

)

;

       

}

       

// Clean up resources

       

private

static

class

ResourceCleaner

implements

Runnable

{

            @Override

           

public

void

run

(

)

{

               

System

.

out

.

println

(

"Cleaning up resource..."

)

;

           

}

       

}

        @Override

       

public

void

close

(

)

{

           

// Actively release resources, cancel the cleanup task registered with Cleaner

            cleanable.

clean

(

)

;

           

System

.

out

.

println

(

"Resource closed"

)

;

       

}

   

}

   

public

static

void

main

(

String

[

]

args

)

{

       

// Use the resource

       

try

(

Resource resource

=

new

Resource

(

)

)

{

           

System

.

out

.

println

(

"Using resource..."

)

;

       

}

// close() is called automatically, manually clean up resources

       

// If close() is not called, GC will execute the cleanup task

       

new

Resource

(

)

;

// Do not call close(), wait for GC to trigger cleanup

       

System

.

gc

(

)

;

// Suggest GC to run (cannot guarantee immediate cleanup)

   

}

}

4. Code Explanation

5. Features and Advantages of Cleaner

6. Use Cases for Cleaner

Cleaner Suitable for managing resources that need automatic release, especially when try-with-resources cannot be used. The following are typical application scenarios:

7. Notes

Overall,java.lang.ref.Cleaner is the recommended resource cleanup tool in Java, providing an efficient and reliable resource management approach.