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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 121      */
 122     private static Reference pending = null;
 123 
 124     /* High-priority thread to enqueue pending References
 125      */
 126     private static class ReferenceHandler extends Thread {
 127 
 128         ReferenceHandler(ThreadGroup g, String name) {
 129             super(g, name);
 130         }
 131 
 132         public void run() {
 133             for (;;) {
 134                 Reference r;
 135                 synchronized (lock) {
 136                     if (pending != null) {
 137                         r = pending;
 138                         pending = r.discovered;
 139                         r.discovered = null;
 140                     } else {














 141                         try {
 142                             lock.wait();

 143                         } catch (InterruptedException x) { }
 144                         continue;
 145                     }
 146                 }
 147 
 148                 // Fast path for cleaners
 149                 if (r instanceof Cleaner) {
 150                     ((Cleaner)r).clean();
 151                     continue;
 152                 }
 153 
 154                 ReferenceQueue q = r.queue;
 155                 if (q != ReferenceQueue.NULL) q.enqueue(r);
 156             }
 157         }
 158     }
 159 
 160     static {
 161         ThreadGroup tg = Thread.currentThread().getThreadGroup();
 162         for (ThreadGroup tgn = tg;


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 121      */
 122     private static Reference pending = null;
 123 
 124     /* High-priority thread to enqueue pending References
 125      */
 126     private static class ReferenceHandler extends Thread {
 127 
 128         ReferenceHandler(ThreadGroup g, String name) {
 129             super(g, name);
 130         }
 131 
 132         public void run() {
 133             for (;;) {
 134                 Reference r;
 135                 synchronized (lock) {
 136                     if (pending != null) {
 137                         r = pending;
 138                         pending = r.discovered;
 139                         r.discovered = null;
 140                     } else {
 141                         // The waiting on the lock may cause an OOME because it may try to allocate
 142                         // exception objects, so also catch OOME here to avoid silent exit of the
 143                         // reference handler thread.
 144                         //
 145                         // Explicitly define the order of the two exceptions we catch here
 146                         // when waiting for the lock.
 147                         //
 148                         // We do not want to try to potentially load the InterruptedException class
 149                         // (which would be done if this was its first use, and InterruptedException
 150                         // were checked first) in this situation.
 151                         //
 152                         // This may lead to the VM not ever trying to load the InterruptedException
 153                         // class again.
 154                         try {
 155                             try {
 156                                 lock.wait();
 157                             } catch (OutOfMemoryError x) { }
 158                         } catch (InterruptedException x) { }
 159                         continue;
 160                     }
 161                 }
 162 
 163                 // Fast path for cleaners
 164                 if (r instanceof Cleaner) {
 165                     ((Cleaner)r).clean();
 166                     continue;
 167                 }
 168 
 169                 ReferenceQueue q = r.queue;
 170                 if (q != ReferenceQueue.NULL) q.enqueue(r);
 171             }
 172         }
 173     }
 174 
 175     static {
 176         ThreadGroup tg = Thread.currentThread().getThreadGroup();
 177         for (ThreadGroup tgn = tg;