< prev index next >

src/java.base/share/classes/jdk/internal/ref/CleanerImpl.java

Print this page

        

@@ -74,11 +74,11 @@
     /**
      * Called to get the CleanerImpl for a Cleaner.
      * @param cleaner the cleaner
      * @return the corresponding CleanerImpl
      */
-    static CleanerImpl getCleanerImpl(Cleaner cleaner) {
+    public static CleanerImpl getCleanerImpl(Cleaner cleaner) {
         return cleanerImplAccess.apply(cleaner);
     }
 
     /**
      * Constructor for CleanerImpl.

@@ -145,19 +145,38 @@
                 // due to a race with clear/clean
                 Cleanable ref = (Cleanable) queue.remove(60 * 1000L);
                 if (ref != null) {
                     ref.clean();
                 }
-            } catch (InterruptedException i) {
-                continue;   // ignore the interruption
             } catch (Throwable e) {
                 // ignore exceptions from the cleanup action
+                // (including interruption of cleanup thread)
             }
         }
     }
 
     /**
+     * Processes all Cleanable(s) that have been waiting in the queue.
+     *
+     * @return {@code true} if any Cleanable was found in the queue and
+     * was processed or {@code false} if the queue was empty.
+     */
+    public boolean drainQueue() {
+        boolean cleaned = false;
+        Cleanable ref;
+        while ((ref = (Cleanable) queue.poll()) != null) {
+            try {
+                ref.clean();
+            } catch (Throwable t) {
+                // ignore exceptions from the cleanup action
+            }
+            cleaned = true;
+        }
+        return cleaned;
+    }
+
+    /**
      * Perform cleaning on an unreachable PhantomReference.
      */
     public static final class PhantomCleanableRef extends PhantomCleanable<Object> {
         private final Runnable action;
 
< prev index next >