< prev index next >
src/java.base/share/classes/java/lang/ref/Reference.java
Print this page
*** 25,37 ****
package java.lang.ref;
import jdk.internal.vm.annotation.DontInline;
import jdk.internal.HotSpotIntrinsicCandidate;
- import jdk.internal.misc.JavaLangRefAccess;
- import jdk.internal.misc.SharedSecrets;
- import jdk.internal.ref.Cleaner;
/**
* Abstract base class for reference objects. This class defines the
* operations common to all reference objects. Because reference objects are
* implemented in close cooperation with the garbage collector, this class may
--- 25,34 ----
*** 137,151 ****
throw (Error) new NoClassDefFoundError(e.getMessage()).initCause(e);
}
}
static {
! // pre-load and initialize InterruptedException and Cleaner classes
// so that we don't get into trouble later in the run loop if there's
! // memory shortage while loading/initializing them lazily.
ensureClassInitialized(InterruptedException.class);
- ensureClassInitialized(Cleaner.class);
}
ReferenceHandler(ThreadGroup g, String name) {
super(g, null, name, 0, false);
}
--- 134,147 ----
throw (Error) new NoClassDefFoundError(e.getMessage()).initCause(e);
}
}
static {
! // pre-load and initialize InterruptedException 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(InterruptedException.class);
}
ReferenceHandler(ThreadGroup g, String name) {
super(g, null, name, 0, false);
}
*** 173,190 ****
* or thread was interrupted before being notified;
* {@code false} otherwise.
*/
static boolean tryHandlePending(boolean waitForNotify) {
Reference<Object> r;
- Cleaner c;
try {
synchronized (lock) {
- if (pending != null) {
r = pending;
! // 'instanceof' might throw OutOfMemoryError sometimes
! // so do this before un-linking 'r' from the 'pending' chain...
! c = r instanceof Cleaner ? (Cleaner) r : null;
// unlink 'r' from 'pending' chain
pending = r.discovered;
r.discovered = null;
} else {
// The waiting on the lock may cause an OutOfMemoryError
--- 169,182 ----
* or thread was interrupted before being notified;
* {@code false} otherwise.
*/
static boolean tryHandlePending(boolean waitForNotify) {
Reference<Object> r;
try {
synchronized (lock) {
r = pending;
! if (r != null) {
// unlink 'r' from 'pending' chain
pending = r.discovered;
r.discovered = null;
} else {
// The waiting on the lock may cause an OutOfMemoryError
*** 197,222 ****
}
}
} catch (OutOfMemoryError x) {
// Give other threads CPU time so they hopefully drop some live references
// and GC reclaims some space.
- // Also prevent CPU intensive spinning in case 'r instanceof Cleaner' above
- // persistently throws OOME for some time...
Thread.yield();
// retry
return true;
} catch (InterruptedException x) {
// retry
return true;
}
- // Fast path for cleaners
- if (c != null) {
- c.clean();
- return true;
- }
-
ReferenceQueue<? super Object> q = r.queue;
if (q != ReferenceQueue.NULL) q.enqueue(r);
return true;
}
--- 189,206 ----
*** 230,247 ****
* MAX_PRIORITY, it would be used here
*/
handler.setPriority(Thread.MAX_PRIORITY);
handler.setDaemon(true);
handler.start();
-
- // provide access in SharedSecrets
- SharedSecrets.setJavaLangRefAccess(new JavaLangRefAccess() {
- @Override
- public boolean tryHandlePendingReference() {
- return tryHandlePending(false);
- }
- });
}
/* -- Referent accessor and setters -- */
/**
--- 214,223 ----
< prev index next >