< prev index next >

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

Print this page
@@ -23,10 +23,12 @@
   * questions.
   */
  
  package java.lang.ref;
  
+ import jdk.internal.misc.Unsafe;
+ import jdk.internal.misc.VM;
  import jdk.internal.vm.annotation.ForceInline;
  import jdk.internal.vm.annotation.IntrinsicCandidate;
  import jdk.internal.access.JavaLangRefAccess;
  import jdk.internal.access.SharedSecrets;
  import jdk.internal.ref.Cleaner;

@@ -189,30 +191,30 @@
  
      /* High-priority thread to enqueue pending References
       */
      private static class ReferenceHandler extends Thread {
  
-         private static void ensureClassInitialized(Class<?> clazz) {
-             try {
-                 Class.forName(clazz.getName(), true, clazz.getClassLoader());
-             } catch (ClassNotFoundException e) {
-                 throw (Error) new NoClassDefFoundError(e.getMessage()).initCause(e);
-             }
+         ReferenceHandler(ThreadGroup g, String name) {
+             super(g, null, name, 0, false);
          }
  
-         static {
+         public void run() {
+ 
+             // delay until init phase 1 completes
+             while (VM.initLevel() < 1) {
+                 try {
+                     VM.awaitInitLevel(1);
+                 } catch (InterruptedException x) {
+                     // ignore and continue
+                 }
+             }
+ 
              // pre-load and initialize Cleaner class so that we don't
              // get into trouble later in the run loop if there's
              // memory shortage while loading/initializing it lazily.
-             ensureClassInitialized(Cleaner.class);
-         }
+             Unsafe.getUnsafe().ensureClassInitialized(Cleaner.class);
  
-         ReferenceHandler(ThreadGroup g, String name) {
-             super(g, null, name, 0, false);
-         }
- 
-         public void run() {
              while (true) {
                  processPendingReferences();
              }
          }
      }

@@ -299,10 +301,11 @@
              }
          }
      }
  
      static {
+         @SuppressWarnings("deprecation")
          ThreadGroup tg = Thread.currentThread().getThreadGroup();
          for (ThreadGroup tgn = tg;
               tgn != null;
               tg = tgn, tgn = tg.getParent());
          Thread handler = new ReferenceHandler(tg, "Reference Handler");

@@ -324,10 +327,15 @@
  
              @Override
              public void runFinalization() {
                  Finalizer.runFinalization();
              }
+ 
+             @Override
+             public <T> ReferenceQueue<T> newNativeReferenceQueue() {
+                 return new NativeReferenceQueue<T>();
+             }
          });
      }
  
      /* -- Referent accessor and setters -- */
  
< prev index next >