--- old/src/java.base/share/classes/java/lang/ref/Cleaner.java 2016-02-21 19:30:02.036322790 +0100 +++ new/src/java.base/share/classes/java/lang/ref/Cleaner.java 2016-02-21 19:30:01.931324621 +0100 @@ -25,10 +25,11 @@ package java.lang.ref; +import jdk.internal.ref.CleanerImpl; + import java.util.Objects; import java.util.concurrent.ThreadFactory; - -import jdk.internal.ref.CleanerImpl; +import java.util.function.Function; /** * {@code Cleaner} manages a set of object references and corresponding cleaning actions. @@ -135,7 +136,12 @@ final CleanerImpl impl; static { - CleanerImpl.setCleanerImplAccess((Cleaner c) -> c.impl); + CleanerImpl.setCleanerImplAccess(new Function() { + @Override + public CleanerImpl apply(Cleaner cleaner) { + return cleaner.impl; + } + }); } /** @@ -215,6 +221,32 @@ } /** + * Helps cleaning thread by invoking next cleaning action registered by this + * Cleaner and for which GC has determined that the monitored object has become + * phantom reachable. + * + * @return {@code true} if a cleaning action was invoked by this method or + * {@code false} if there were no cleaning actions registered by this Cleaner + * for which GC would determine that their monitored objects are phantom reachable. + * + * @implNote Normally this method needs not be called as cleaning actions are + * executed by a background thread. But it is sometimes necessary to help the + * background thread with cleanup when it can not keep-up with the rate of + * registration of new cleaning actions and consequently the resource + * being cleaned-up may run-out. When this happens the resource allocation logic + * can re-try the allocation while helping with cleanup until this method + * returns false indicating that all pending cleaning actions have been invoked. + */ + public boolean helpClean() { + while (!impl.cleanNextEnqueuedCleanable()) { + if (!Reference.tryHandlePending(false)) { + return false; + } + } + return true; + } + + /** * {@code Cleanable} represents an object and a * cleaning action registered in a {@code Cleaner}. * @since 9