< prev index next >

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

Print this page

        

@@ -27,11 +27,10 @@
 
 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}

@@ -126,32 +125,11 @@
  * 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();
-    }
+public interface Cleaner {
 
     /**
      * Returns a new {@code Cleaner}.
      * <p>
      * The cleaner creates a {@link Thread#setDaemon(boolean) daemon thread}

@@ -168,14 +146,12 @@
      * @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;
+    static Cleaner create() {
+        return new CleanerImpl(null);
     }
 
     /**
      * Returns a new {@code Cleaner} using a {@code Thread} from the {@code ThreadFactory}.
      * <p>

@@ -195,15 +171,13 @@
      * @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) {
+    static Cleaner create(ThreadFactory threadFactory) {
         Objects.requireNonNull(threadFactory, "threadFactory");
-        Cleaner cleaner = new Cleaner();
-        cleaner.impl.start(cleaner, threadFactory);
-        return cleaner;
+        return new CleanerImpl(threadFactory);
     }
 
     /**
      * Registers an object and a cleaning action to run when the object
      * becomes phantom reachable.

@@ -212,22 +186,18 @@
      *
      * @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);
-    }
+    Cleanable register(Object obj, Runnable action);
 
     /**
      * {@code Cleanable} represents an object and a
      * cleaning action registered in a {@code Cleaner}.
      * @since 9
      */
-    public interface Cleanable {
+    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 >