< 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 the background cleaning thread by invoking next cleaning action + * {@link #register(Object, Runnable) registered} by this Cleaner and for + * which GC has determined that the monitored object is phantom reachable. + * + * @return {@code true} if next cleaning action was invoked by this method or + * {@code false} if there was no cleaning action registered by this Cleaner + * for which GC had determined that its monitored object was phantom reachable. + * + * @implNote Normally this method needs not be called as cleaning actions are + * executed by a background thread. But in some situations, when cleaning + * actions take relatively long time to complete and are not invoked explicitly + * by calling {@link Cleanable#clean()}, it might be necessary to help the + * background thread when it can't keep-up with the rate of registration of + * new cleaning actions. + * <p> + * For example, a program can decide to help with cleanup after it fails to + * allocate some limited resource for which it registered cleaning actions. + * After giving some help to the background thread, it can re-try to allocate + * the resource. Such strategy devotes more CPU time to cleanup when needed + * and provides back-pressure to threads allocating the limited resource. + */ + boolean cleanNextPending() { + return impl.cleanNextPending(); + } + + /** * {@code Cleanable} represents an object and a * cleaning action registered in a {@code Cleaner}. * @since 9 */ public interface Cleanable {
< prev index next >