--- /dev/null 2016-01-17 08:29:16.562151811 +0100 +++ new/src/java.base/share/classes/java/lang/ref/Ephemeron.java 2016-01-17 17:01:38.454324364 +0100 @@ -0,0 +1,156 @@ +/* + * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package java.lang.ref; + + +/** + * Ephemeron objects are a special kind of Reference objects, which do not prevent + * their referents, also called keys, from being made finalizable, finalized, and + * then reclaimed. In addition to the key, an ephemeron also holds a value which + * is treated specially by garbage collector. Unless the value is reachable in some + * other way other than through the ephemeron, it is considered + * weakly reachable if and only if the key of the same ephemeron is + * strongly, softly, or weakly reachable otherwise the value is + * considered ephemerally reachable. + *

Suppose that the garbage collector determines at a certain point in time + * that an object is ephemerally reachable. + * At that time it will atomically clear all ephemerons that refer to this object + * through their keys and all ephemerons that refer to other ephemerally-reachable + * objects from which that object is reachable through a chain of strong, soft or + * weak references. + * At the same time garbage collector will declare all of the formerly + * ephemerally-reachable objects to be finalizable. At the same time or at some + * later time it will enqueue those newly-cleared ephemerons that are + * registered with reference queues. + *

By convenience, the Ephemeron's referent is also called the key, and can be + * obtained either by invoking {@link #get} or {@link #getKey} while the value + * can be obtained by invoking {@link #getValue} method. + * + *

Reachability

+ * Going from strongest to weakest, the different levels of + * reachability reflect the life cycle of an object. They are + * operationally defined as follows: + * + * + * @author Peter Levart + * @since 9 + */ + +public class Ephemeron extends Reference { + + /** + * The value field is treated specially by GC + */ + private V value; + + /** + * Creates a new ephemeron that refers to the given key and + * given value. The new ephemeron is not registered with any queue. + * + * @param key the key (or referent) the new ephemeron will refer to + * @param value the value the new ephemeron will hold until cleared + */ + public Ephemeron(K key, V value) { + super(key); + this.value = value; + } + + /** + * Creates a new ephemeron that refers to the given key and + * given value and is registered with the given queue. + * + * @param key the key (or referent) the new ephemeron will refer to + * @param value the value the new ephemeron will hold until cleared + * @param q the queue with which the ephemeron is to be registered, + * or {@code null} if registration is not required + */ + public Ephemeron(K key, V value, ReferenceQueue q) { + super(key, q); + this.value = value; + } + + /** + * Returns this ephemeron object's referent, also known as the key. + * If this ephemeron object has been cleared, either by the program or by + * the garbage collector, then this method returns null. + * + * @return The key object to which this ephemeron object refers, or + * null if this ephemeron object has been cleared + * @see #getValue() + */ + public K getKey() { + return get(); + } + + /** + * Returns this ephemeron object's value. + * If this ephemeron object has been cleared, either by the program or by + * the garbage collector, then this method returns null. + * + * @return The value object to which this ephemeron object refers, or + * null if this ephemeron object has been cleared + * @see #getKey() + */ + public V getValue() { + return value; + } + + /** + * Clears this ephemeron object, resetting both the key and + * the value to null. Invoking this method will not cause this + * ephemeron object to be enqueued. + *

This method is invoked only by Java code; when the garbage collector + * clears ephemerons it does so directly, without invoking this method. + */ + @Override + public void clear() { + super.clear(); + this.value = null; + } +}