8003335: Better handling of Finalizer thread
authorchegar
Thu, 20 Dec 2012 13:40:27 +0000
changeset 16875 a29830a7e5b3
parent 16874 acf47f31a0da
child 16876 17f574546dc8
8003335: Better handling of Finalizer thread Reviewed-by: alanb, ahgross
jdk/src/share/classes/java/lang/ref/Finalizer.java
--- a/jdk/src/share/classes/java/lang/ref/Finalizer.java	Tue Feb 05 16:38:25 2013 -0800
+++ b/jdk/src/share/classes/java/lang/ref/Finalizer.java	Thu Dec 20 13:40:27 2012 +0000
@@ -38,9 +38,9 @@
      */
     static native void invokeFinalizeMethod(Object o) throws Throwable;
 
-    static private ReferenceQueue queue = new ReferenceQueue();
-    static private Finalizer unfinalized = null;
-    static private Object lock = new Object();
+    private static ReferenceQueue queue = new ReferenceQueue();
+    private static Finalizer unfinalized = null;
+    private static final Object lock = new Object();
 
     private Finalizer
         next = null,
@@ -142,7 +142,11 @@
     /* Called by Runtime.runFinalization() */
     static void runFinalization() {
         forkSecondaryFinalizer(new Runnable() {
+            private volatile boolean running;
             public void run() {
+                if (running)
+                    return;
+                running = true;
                 for (;;) {
                     Finalizer f = (Finalizer)queue.poll();
                     if (f == null) break;
@@ -155,7 +159,11 @@
     /* Invoked by java.lang.Shutdown */
     static void runAllFinalizers() {
         forkSecondaryFinalizer(new Runnable() {
+            private volatile boolean running;
             public void run() {
+                if (running)
+                    return;
+                running = true;
                 for (;;) {
                     Finalizer f;
                     synchronized (lock) {
@@ -168,10 +176,14 @@
     }
 
     private static class FinalizerThread extends Thread {
+        private volatile boolean running;
         FinalizerThread(ThreadGroup g) {
             super(g, "Finalizer");
         }
         public void run() {
+            if (running)
+                return;
+            running = true;
             for (;;) {
                 try {
                     Finalizer f = (Finalizer)queue.remove();