< prev index next >

src/java.base/share/classes/java/lang/ref/Cleaner.java

Print this page

        

*** 23,36 **** * questions. */ package java.lang.ref; import java.util.Objects; import java.util.concurrent.ThreadFactory; ! ! import jdk.internal.ref.CleanerImpl; /** * {@code Cleaner} manages a set of object references and corresponding cleaning actions. * <p> * Cleaning actions are {@link #register(Object object, Runnable action) registered} --- 23,37 ---- * questions. */ package java.lang.ref; + import jdk.internal.ref.CleanerImpl; + import java.util.Objects; import java.util.concurrent.ThreadFactory; ! import java.util.function.Function; /** * {@code Cleaner} manages a set of object references and corresponding cleaning actions. * <p> * Cleaning actions are {@link #register(Object object, Runnable action) registered}
*** 133,143 **** * The Cleaner implementation. */ final CleanerImpl impl; static { ! CleanerImpl.setCleanerImplAccess((Cleaner c) -> c.impl); } /** * Construct a Cleaner implementation and start it. */ --- 134,149 ---- * The Cleaner implementation. */ final CleanerImpl impl; static { ! CleanerImpl.setCleanerImplAccess(new Function<Cleaner, CleanerImpl>() { ! @Override ! public CleanerImpl apply(Cleaner cleaner) { ! return cleaner.impl; ! } ! }); } /** * Construct a Cleaner implementation and start it. */
*** 213,222 **** --- 219,254 ---- Objects.requireNonNull(action, "action"); return new CleanerImpl.PhantomCleanableRef(obj, this, action); } /** + * 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 */ public interface Cleanable {
< prev index next >