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                         // we explicitly cascade the two exceptions here to avoid
 142                         // double throw of OOME in case InterruptedException needs
 143                         // to be loaded during OOME caused by waiting for the
 144                         // lock to be signaled.
 145                         // The class loading may further trigger other secondary
 146                         // exceptions due to low memory that may cause the thread
 147                         // to terminate too.
 148                         try {
 149                             try {
 150                                 lock.wait();
 151                             } catch (OutOfMemoryError x) { }
 152                         } catch (InterruptedException x) { }
 153                         continue;
 154                     }
 155                 }
 156 
 157                 // Fast path for cleaners
 158                 if (r instanceof Cleaner) {
 159                     ((Cleaner)r).clean();
 160                     continue;
 161                 }
 162 
 163                 ReferenceQueue q = r.queue;
 164                 if (q != ReferenceQueue.NULL) q.enqueue(r);
 165             }
 166         }
 167     }
 168 
 169     static {
 170         ThreadGroup tg = Thread.currentThread().getThreadGroup();
 171         for (ThreadGroup tgn = tg;