Search in sources :

Example 1 with Cleanable

use of java.lang.ref.Cleaner.Cleanable in project Bytecoder by mirkosertic.

the class CleanerImpl method run.

/**
 * Process queued Cleanables as long as the cleanable lists are not empty.
 * A Cleanable is in one of the lists for each Object and for the Cleaner
 * itself.
 * Terminates when the Cleaner is no longer reachable and
 * has been cleaned and there are no more Cleanable instances
 * for which the object is reachable.
 * <p>
 * If the thread is a ManagedLocalsThread, the threadlocals
 * are erased before each cleanup
 */
@Override
public void run() {
    Thread t = Thread.currentThread();
    InnocuousThread mlThread = (t instanceof InnocuousThread) ? (InnocuousThread) t : null;
    while (!phantomCleanableList.isListEmpty() || !weakCleanableList.isListEmpty() || !softCleanableList.isListEmpty()) {
        if (mlThread != null) {
            // Clear the thread locals
            mlThread.eraseThreadLocals();
        }
        try {
            // Wait for a Ref, with a timeout to avoid getting hung
            // due to a race with clear/clean
            Cleanable ref = (Cleanable) queue.remove(60 * 1000L);
            if (ref != null) {
                ref.clean();
            }
        } catch (Throwable e) {
        // ignore exceptions from the cleanup action
        // (including interruption of cleanup thread)
        }
    }
}
Also used : InnocuousThread(jdk.internal.misc.InnocuousThread) Cleanable(java.lang.ref.Cleaner.Cleanable) InnocuousThread(jdk.internal.misc.InnocuousThread)

Example 2 with Cleanable

use of java.lang.ref.Cleaner.Cleanable in project openj9 by eclipse.

the class CleanerShutdown method shutdownCleaner.

public static void shutdownCleaner() {
    Cleaner commonCleaner = CleanerFactory.cleaner();
    CleanerImpl commonCleanerImpl = CleanerImpl.getCleanerImpl(commonCleaner);
    Cleanable ref = null;
    while ((ref = (Cleanable) commonCleanerImpl.queue.poll()) != null) {
        try {
            ref.clean();
        } catch (Throwable t) {
        /* do nothing */
        }
    }
    try {
        // $NON-NLS-1$
        Method phantomRemove = PhantomCleanable.class.getDeclaredMethod("remove", (Class<?>[]) null);
        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
            phantomRemove.setAccessible(true);
            return null;
        });
        while (!commonCleanerImpl.phantomCleanableList.isListEmpty()) {
            phantomRemove.invoke(commonCleanerImpl.phantomCleanableList, (Object[]) null);
        }
    } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        /* should not fail */
        e.printStackTrace();
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Cleanable(java.lang.ref.Cleaner.Cleanable) Cleaner(java.lang.ref.Cleaner)

Aggregations

Cleanable (java.lang.ref.Cleaner.Cleanable)2 Cleaner (java.lang.ref.Cleaner)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 InnocuousThread (jdk.internal.misc.InnocuousThread)1