简体   繁体   English

Java 9 Cleaner正确用法

[英]Java 9 Cleaner Correct Usage

Reading about Java 9's Cleaner class, I found this example in the same page: 阅读Java 9的Cleaner类,我在同一页面中找到了这个例子:

public class CleaningExample implements AutoCloseable {
    // A cleaner, preferably one shared within a library
    private static final Cleaner cleaner = <cleaner>;

    static class State implements Runnable {

        State(...) {
            // initialize State needed for cleaning action
        }

        public void run() {
            // cleanup action accessing State, executed at most once
        }
    }

    private final State;
    private final Cleaner.Cleanable cleanable

    public CleaningExample() {
        this.state = new State(...);
        this.cleanable = cleaner.register(this, state);
    }

    public void close() {
        cleanable.clean();
    }
}

In the second line there is a comment saying: 在第二行有评论说:

A cleaner, preferably one shared within a library 清洁工,最好是在图书馆内共享的清洁工

Why is it preferable to have one shared Cleaner (static) within a library? 为什么在库中有一个共享Cleaner (静态)更可取?

Does anybody have a good example about how to use Cleaner instead of overriding finalize() ? 有没有人有一个关于如何使用Cleaner而不是覆盖finalize()的好例子?

Why is it preferable to have one shared Cleaner (static) within a library? 为什么在库中有一个共享清理程序(静态)更可取?

A cleaner has an associated thread. 清洁工有一个相关的线程。 Threads are limited native resources. 线程是有限的本机资源。 So the goal is to limit the amount of Threads created by not creating more cleaners than necessary. 因此,目标是通过不创建超过必要的清洁程序来限制创建的线程数量。

Does anybody have a good example about how to use Cleaner instead of overriding finalize()? 有没有人有一个关于如何使用Cleaner而不是覆盖finalize()的好例子?

You posted the reference example. 您发布了参考示例。 You need to ask more specific questions if that is insufficient. 如果不充分,您需要询问更具体的问题。

The docs do explicitly mention: 文档明确提到:

The choice of a new cleaner or sharing an existing cleaner is determined by the use case. 选择新的清洁剂或共享现有的清洁剂由用例决定。

Also: 也:

Each cleaner operates independently, managing the pending [...] 每个清洁工独立运作,管理待处理的[...]

which implies multiple Cleaner instances are allowed within an application. 这意味着应用程序中允许多个Cleaner实例。

Again, since the factory method Cleaner::create is provided and is documented as 同样,由于提供了工厂方法Cleaner::create并将其记录为

Returns a new Cleaner. 返回一个新的Cleaner。

I do not see why only one Cleaner per application should be used, although the comment definitely states otherwise. 我不明白为什么每个应用程序只应使用一个Cleaner ,尽管评论明确指出。


By surfing the web for a minute I found a few examples ( eg this article ), and all use a static Cleaner for each AutoCloseable sub-class. 通过浏览网页一分钟我发现了一些例子(例如本文 ),并且每个AutoCloseable子类都使用static清理器。

private final static Cleaner cleaner = Cleaner.create();

Hope it help you if using java9. 希望它对你有帮助,如果使用java9。

The code below was already tested in Intellij IDEA 2017 and oracle jdk 9 . 下面的代码已在Intellij IDEA 2017oracle jdk 9中测试过。

import java.lang.ref.Cleaner;

public class Main {

    public Main() {

    }

    public static void main(String[] args) {
        System.out.println("Hello World!");

        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Cleaner cleaner = Cleaner.create();
            Main obj = new Main();
            cleaner.register(obj, new Runnable() {
                @Override
                public void run() {
                    System.out.println("Hello World!222");
                }
            });
            System.gc();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM