6666739: (ref) ReferenceQueue.poll() doesn't scale well

6711667: (ref) Update SoftReference timestamp only if clock advances

Forward port from 6u14; originally fixed by Tom Rodriguez in earlier update

Reviewed-by: martin
This commit is contained in:
Alan Bateman 2009-04-02 11:19:34 +01:00
parent d2cd251815
commit 15bf5db9c7
2 changed files with 9 additions and 4 deletions

View File

@ -51,7 +51,7 @@ public class ReferenceQueue<T> {
static private class Lock { }; static private class Lock { };
private Lock lock = new Lock(); private Lock lock = new Lock();
private Reference<? extends T> head = null; private volatile Reference<? extends T> head = null;
private long queueLength = 0; private long queueLength = 0;
boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */ boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
@ -95,6 +95,8 @@ public class ReferenceQueue<T> {
* otherwise <code>null</code> * otherwise <code>null</code>
*/ */
public Reference<? extends T> poll() { public Reference<? extends T> poll() {
if (head == null)
return null;
synchronized (lock) { synchronized (lock) {
return reallyPoll(); return reallyPoll();
} }

View File

@ -63,11 +63,13 @@ package java.lang.ref;
public class SoftReference<T> extends Reference<T> { public class SoftReference<T> extends Reference<T> {
/* Timestamp clock, updated by the garbage collector /**
* Timestamp clock, updated by the garbage collector
*/ */
static private long clock; static private long clock;
/* Timestamp updated by each invocation of the get method. The VM may use /**
* Timestamp updated by each invocation of the get method. The VM may use
* this field when selecting soft references to be cleared, but it is not * this field when selecting soft references to be cleared, but it is not
* required to do so. * required to do so.
*/ */
@ -108,7 +110,8 @@ public class SoftReference<T> extends Reference<T> {
*/ */
public T get() { public T get() {
T o = super.get(); T o = super.get();
if (o != null) this.timestamp = clock; if (o != null && this.timestamp != clock)
this.timestamp = clock;
return o; return o;
} }