< prev index next >

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

Print this page

        

*** 27,37 **** 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} --- 27,36 ----
*** 126,157 **** * and not block. If the cleaning action blocks, it may delay processing * other cleaning actions registered to the same cleaner. * All cleaning actions registered to a cleaner should be mutually compatible. * @since 9 */ ! public final class Cleaner { ! ! /** ! * 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. ! */ ! private Cleaner() { ! impl = new CleanerImpl(); ! } /** * Returns a new {@code Cleaner}. * <p> * The cleaner creates a {@link Thread#setDaemon(boolean) daemon thread} --- 125,135 ---- * and not block. If the cleaning action blocks, it may delay processing * other cleaning actions registered to the same cleaner. * All cleaning actions registered to a cleaner should be mutually compatible. * @since 9 */ ! public interface Cleaner { /** * Returns a new {@code Cleaner}. * <p> * The cleaner creates a {@link Thread#setDaemon(boolean) daemon thread}
*** 168,181 **** * @return a new {@code Cleaner} * * @throws SecurityException if the current thread is not allowed to * create or start the thread. */ ! public static Cleaner create() { ! Cleaner cleaner = new Cleaner(); ! cleaner.impl.start(cleaner, null); ! return cleaner; } /** * Returns a new {@code Cleaner} using a {@code Thread} from the {@code ThreadFactory}. * <p> --- 146,157 ---- * @return a new {@code Cleaner} * * @throws SecurityException if the current thread is not allowed to * create or start the thread. */ ! static Cleaner create() { ! return new CleanerImpl(null); } /** * Returns a new {@code Cleaner} using a {@code Thread} from the {@code ThreadFactory}. * <p>
*** 195,209 **** * @throws IllegalThreadStateException if the thread from the thread * factory was {@link Thread.State#NEW not a new thread}. * @throws SecurityException if the current thread is not allowed to * create or start the thread. */ ! public static Cleaner create(ThreadFactory threadFactory) { Objects.requireNonNull(threadFactory, "threadFactory"); ! Cleaner cleaner = new Cleaner(); ! cleaner.impl.start(cleaner, threadFactory); ! return cleaner; } /** * Registers an object and a cleaning action to run when the object * becomes phantom reachable. --- 171,183 ---- * @throws IllegalThreadStateException if the thread from the thread * factory was {@link Thread.State#NEW not a new thread}. * @throws SecurityException if the current thread is not allowed to * create or start the thread. */ ! static Cleaner create(ThreadFactory threadFactory) { Objects.requireNonNull(threadFactory, "threadFactory"); ! return new CleanerImpl(threadFactory); } /** * Registers an object and a cleaning action to run when the object * becomes phantom reachable.
*** 212,233 **** * * @param obj the object to monitor * @param action a {@code Runnable} to invoke when the object becomes phantom reachable * @return a {@code Cleanable} instance */ ! public Cleanable register(Object obj, Runnable action) { ! Objects.requireNonNull(obj, "obj"); ! Objects.requireNonNull(action, "action"); ! return new CleanerImpl.PhantomCleanableRef(obj, this, action); ! } /** * {@code Cleanable} represents an object and a * cleaning action registered in a {@code Cleaner}. * @since 9 */ ! public interface Cleanable { /** * Unregisters the cleanable and invokes the cleaning action. * The cleanable's cleaning action is invoked at most once * regardless of the number of calls to {@code clean}. */ --- 186,203 ---- * * @param obj the object to monitor * @param action a {@code Runnable} to invoke when the object becomes phantom reachable * @return a {@code Cleanable} instance */ ! Cleanable register(Object obj, Runnable action); /** * {@code Cleanable} represents an object and a * cleaning action registered in a {@code Cleaner}. * @since 9 */ ! interface Cleanable { /** * Unregisters the cleanable and invokes the cleaning action. * The cleanable's cleaning action is invoked at most once * regardless of the number of calls to {@code clean}. */
< prev index next >