< prev index next >

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

Print this page




 104             }
 105         } catch (Throwable x) { }
 106         super.clear();
 107     }
 108 
 109     /* Create a privileged secondary finalizer thread in the system thread
 110        group for the given Runnable, and wait for it to complete.
 111 
 112        This method is used by both runFinalization and runFinalizersOnExit.
 113        The former method invokes all pending finalizers, while the latter
 114        invokes all uninvoked finalizers if on-exit finalization has been
 115        enabled.
 116 
 117        These two methods could have been implemented by offloading their work
 118        to the regular finalizer thread and waiting for that thread to finish.
 119        The advantage of creating a fresh thread, however, is that it insulates
 120        invokers of these methods from a stalled or deadlocked finalizer thread.
 121      */
 122     private static void forkSecondaryFinalizer(final Runnable proc) {
 123         AccessController.doPrivileged(
 124             new PrivilegedAction<Void>() {
 125                 public Void run() {
 126                     ThreadGroup tg = Thread.currentThread().getThreadGroup();
 127                     for (ThreadGroup tgn = tg;
 128                          tgn != null;
 129                          tg = tgn, tgn = tg.getParent());
 130                     Thread sft = new ManagedLocalsThread(tg, proc, "Secondary finalizer");
 131                     sft.start();
 132                     try {
 133                         sft.join();
 134                     } catch (InterruptedException x) {
 135                         Thread.currentThread().interrupt();
 136                     }
 137                     return null;
 138                 }});
 139     }
 140 
 141     /* Called by Runtime.runFinalization() */
 142     static void runFinalization() {
 143         if (!VM.isBooted()) {
 144             return;




 104             }
 105         } catch (Throwable x) { }
 106         super.clear();
 107     }
 108 
 109     /* Create a privileged secondary finalizer thread in the system thread
 110        group for the given Runnable, and wait for it to complete.
 111 
 112        This method is used by both runFinalization and runFinalizersOnExit.
 113        The former method invokes all pending finalizers, while the latter
 114        invokes all uninvoked finalizers if on-exit finalization has been
 115        enabled.
 116 
 117        These two methods could have been implemented by offloading their work
 118        to the regular finalizer thread and waiting for that thread to finish.
 119        The advantage of creating a fresh thread, however, is that it insulates
 120        invokers of these methods from a stalled or deadlocked finalizer thread.
 121      */
 122     private static void forkSecondaryFinalizer(final Runnable proc) {
 123         AccessController.doPrivileged(
 124             new PrivilegedAction<>() {
 125                 public Void run() {
 126                     ThreadGroup tg = Thread.currentThread().getThreadGroup();
 127                     for (ThreadGroup tgn = tg;
 128                          tgn != null;
 129                          tg = tgn, tgn = tg.getParent());
 130                     Thread sft = new ManagedLocalsThread(tg, proc, "Secondary finalizer");
 131                     sft.start();
 132                     try {
 133                         sft.join();
 134                     } catch (InterruptedException x) {
 135                         Thread.currentThread().interrupt();
 136                     }
 137                     return null;
 138                 }});
 139     }
 140 
 141     /* Called by Runtime.runFinalization() */
 142     static void runFinalization() {
 143         if (!VM.isBooted()) {
 144             return;


< prev index next >