src/java.base/share/classes/java/lang/Object.java
author rriggs
Wed, 28 Nov 2018 15:53:49 -0500
changeset 52724 0bdbf854472f
parent 51758 6c956c883137
child 52953 b75a44aad06c
permissions -rw-r--r--
4947890: Minimize JNI upcalls in system-properties initialization Reviewed-by: erikj, mchung, bchristi, ihse, coleenp, stuefe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51758
6c956c883137 8210787: Object.wait(long, int) throws inappropriate IllegalArgumentException
igerasim
parents: 50474
diff changeset
     2
 * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1937
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1937
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1937
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1937
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1937
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28685
diff changeset
    28
import jdk.internal.HotSpotIntrinsicCandidate;
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28685
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
    31
 * Class {@code Object} is the root of the class hierarchy.
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
    32
 * Every class has {@code Object} as a superclass. All objects,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * including arrays, implement the methods of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @see     java.lang.Class
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 14342
diff changeset
    37
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class Object {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static native void registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
28685
6e8154707178 8071959: java.lang.Object uses implicit default constructor
darcy
parents: 28683
diff changeset
    47
     * Constructs a new object.
6e8154707178 8071959: java.lang.Object uses implicit default constructor
darcy
parents: 28683
diff changeset
    48
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28685
diff changeset
    49
    @HotSpotIntrinsicCandidate
28685
6e8154707178 8071959: java.lang.Object uses implicit default constructor
darcy
parents: 28683
diff changeset
    50
    public Object() {}
6e8154707178 8071959: java.lang.Object uses implicit default constructor
darcy
parents: 28683
diff changeset
    51
6e8154707178 8071959: java.lang.Object uses implicit default constructor
darcy
parents: 28683
diff changeset
    52
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Returns the runtime class of this {@code Object}. The returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * {@code Class} object is the object that is locked by {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * static synchronized} methods of the represented class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * <p><b>The actual result type is {@code Class<? extends |X|>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * where {@code |X|} is the erasure of the static type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * expression on which {@code getClass} is called.</b> For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * example, no cast is required in this code fragment:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * {@code Number n = 0;                             }<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * {@code Class<? extends Number> c = n.getClass(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @return The {@code Class} object that represents the runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *         class of this object.
11516
0e4924734eed 7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents: 9266
diff changeset
    69
     * @jls 15.8.2 Class Literals
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28685
diff changeset
    71
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public final native Class<?> getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns a hash code value for the object. This method is
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
    76
     * supported for the benefit of hash tables such as those provided by
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
    77
     * {@link java.util.HashMap}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <p>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
    79
     * The general contract of {@code hashCode} is:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <li>Whenever it is invoked on the same object more than once during
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
    82
     *     an execution of a Java application, the {@code hashCode} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *     must consistently return the same integer, provided no information
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
    84
     *     used in {@code equals} comparisons on the object is modified.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *     This integer need not remain consistent from one execution of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *     application to another execution of the same application.
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
    87
     * <li>If two objects are equal according to the {@code equals(Object)}
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
    88
     *     method, then calling the {@code hashCode} method on each of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *     the two objects must produce the same integer result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * <li>It is <em>not</em> required that if two objects are unequal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *     according to the {@link java.lang.Object#equals(java.lang.Object)}
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
    92
     *     method, then calling the {@code hashCode} method on each of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *     two objects must produce distinct integer results.  However, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *     programmer should be aware that producing distinct integer results
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
    95
     *     for unequal objects may improve the performance of hash tables.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <p>
28683
a36c179aa292 8071434: doc updates for java.lang.Object
darcy
parents: 27733
diff changeset
    98
     * As much as is reasonably practical, the hashCode method defined
a36c179aa292 8071434: doc updates for java.lang.Object
darcy
parents: 27733
diff changeset
    99
     * by class {@code Object} does return distinct integers for
a36c179aa292 8071434: doc updates for java.lang.Object
darcy
parents: 27733
diff changeset
   100
     * distinct objects. (The hashCode may or may not be implemented
a36c179aa292 8071434: doc updates for java.lang.Object
darcy
parents: 27733
diff changeset
   101
     * as some function of an object's memory address at some point
a36c179aa292 8071434: doc updates for java.lang.Object
darcy
parents: 27733
diff changeset
   102
     * in time.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @return  a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @see     java.lang.Object#equals(java.lang.Object)
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   106
     * @see     java.lang.System#identityHashCode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28685
diff changeset
   108
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public native int hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Indicates whether some other object is "equal to" this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <p>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   114
     * The {@code equals} method implements an equivalence relation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * on non-null object references:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <li>It is <i>reflexive</i>: for any non-null reference value
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   118
     *     {@code x}, {@code x.equals(x)} should return
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   119
     *     {@code true}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <li>It is <i>symmetric</i>: for any non-null reference values
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   121
     *     {@code x} and {@code y}, {@code x.equals(y)}
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   122
     *     should return {@code true} if and only if
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   123
     *     {@code y.equals(x)} returns {@code true}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <li>It is <i>transitive</i>: for any non-null reference values
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   125
     *     {@code x}, {@code y}, and {@code z}, if
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   126
     *     {@code x.equals(y)} returns {@code true} and
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   127
     *     {@code y.equals(z)} returns {@code true}, then
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   128
     *     {@code x.equals(z)} should return {@code true}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <li>It is <i>consistent</i>: for any non-null reference values
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   130
     *     {@code x} and {@code y}, multiple invocations of
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   131
     *     {@code x.equals(y)} consistently return {@code true}
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   132
     *     or consistently return {@code false}, provided no
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   133
     *     information used in {@code equals} comparisons on the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *     objects is modified.
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   135
     * <li>For any non-null reference value {@code x},
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   136
     *     {@code x.equals(null)} should return {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <p>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   139
     * The {@code equals} method for class {@code Object} implements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * the most discriminating possible equivalence relation on objects;
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   141
     * that is, for any non-null reference values {@code x} and
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   142
     * {@code y}, this method returns {@code true} if and only
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   143
     * if {@code x} and {@code y} refer to the same object
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   144
     * ({@code x == y} has the value {@code true}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <p>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   146
     * Note that it is generally necessary to override the {@code hashCode}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * method whenever this method is overridden, so as to maintain the
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   148
     * general contract for the {@code hashCode} method, which states
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * that equal objects must have equal hash codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param   obj   the reference object with which to compare.
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   152
     * @return  {@code true} if this object is the same as the obj
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   153
     *          argument; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @see     #hashCode()
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   155
     * @see     java.util.HashMap
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return (this == obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Creates and returns a copy of this object.  The precise meaning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * of "copy" may depend on the class of the object. The general
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   164
     * intent is that, for any object {@code x}, the expression:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * x.clone() != x</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * will be true, and that the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * x.clone().getClass() == x.getClass()</pre></blockquote>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   172
     * will be {@code true}, but these are not absolute requirements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * While it is typically the case that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * x.clone().equals(x)</pre></blockquote>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   177
     * will be {@code true}, this is not an absolute requirement.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * By convention, the returned object should be obtained by calling
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   180
     * {@code super.clone}.  If a class and all of its superclasses (except
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   181
     * {@code Object}) obey this convention, it will be the case that
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   182
     * {@code x.clone().getClass() == x.getClass()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * By convention, the object returned by this method should be independent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * of this object (which is being cloned).  To achieve this independence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * it may be necessary to modify one or more fields of the object returned
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   187
     * by {@code super.clone} before returning it.  Typically, this means
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * copying any mutable objects that comprise the internal "deep structure"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * of the object being cloned and replacing the references to these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * objects with references to the copies.  If a class contains only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * primitive fields or references to immutable objects, then it is usually
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   192
     * the case that no fields in the object returned by {@code super.clone}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * need to be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <p>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   195
     * The method {@code clone} for class {@code Object} performs a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * specific cloning operation. First, if the class of this object does
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   197
     * not implement the interface {@code Cloneable}, then a
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   198
     * {@code CloneNotSupportedException} is thrown. Note that all arrays
6527
352bf578a6dd 4881419: The type of X[].clone() should be X[]
darcy
parents: 5506
diff changeset
   199
     * are considered to implement the interface {@code Cloneable} and that
352bf578a6dd 4881419: The type of X[].clone() should be X[]
darcy
parents: 5506
diff changeset
   200
     * the return type of the {@code clone} method of an array type {@code T[]}
352bf578a6dd 4881419: The type of X[].clone() should be X[]
darcy
parents: 5506
diff changeset
   201
     * is {@code T[]} where T is any reference or primitive type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Otherwise, this method creates a new instance of the class of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * object and initializes all its fields with exactly the contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * the corresponding fields of this object, as if by assignment; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * contents of the fields are not themselves cloned. Thus, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * performs a "shallow copy" of this object, not a "deep copy" operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   208
     * The class {@code Object} does not itself implement the interface
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   209
     * {@code Cloneable}, so calling the {@code clone} method on an object
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   210
     * whose class is {@code Object} will result in throwing an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * exception at run time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return     a clone of this instance.
11516
0e4924734eed 7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents: 9266
diff changeset
   214
     * @throws  CloneNotSupportedException  if the object's class does not
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   215
     *               support the {@code Cloneable} interface. Subclasses
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   216
     *               that override the {@code clone} method can also
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *               throw this exception to indicate that an instance cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *               be cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see java.lang.Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28685
diff changeset
   221
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    protected native Object clone() throws CloneNotSupportedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Returns a string representation of the object. In general, the
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   226
     * {@code toString} method returns a string that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * "textually represents" this object. The result should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * be a concise but informative representation that is easy for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * person to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * It is recommended that all subclasses override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <p>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   232
     * The {@code toString} method for class {@code Object}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * returns a string consisting of the name of the class of which the
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   234
     * object is an instance, the at-sign character `{@code @}', and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * the unsigned hexadecimal representation of the hash code of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * object. In other words, this method returns a string equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * getClass().getName() + '@' + Integer.toHexString(hashCode())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return  a string representation of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Wakes up a single thread that is waiting on this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * monitor. If any threads are waiting on this object, one of them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * is chosen to be awakened. The choice is arbitrary and occurs at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * the discretion of the implementation. A thread waits on an object's
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   254
     * monitor by calling one of the {@code wait} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * The awakened thread will not be able to proceed until the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * thread relinquishes the lock on this object. The awakened thread will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * compete in the usual manner with any other threads that might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * actively competing to synchronize on this object; for example, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * awakened thread enjoys no reliable privilege or disadvantage in being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * the next thread to lock this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * This method should only be called by a thread that is the owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * of this object's monitor. A thread becomes the owner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * object's monitor in one of three ways:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <li>By executing a synchronized instance method of that object.
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   268
     * <li>By executing the body of a {@code synchronized} statement
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *     that synchronizes on the object.
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   270
     * <li>For objects of type {@code Class,} by executing a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *     synchronized static method of that class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Only one thread at a time can own an object's monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
11516
0e4924734eed 7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents: 9266
diff changeset
   276
     * @throws  IllegalMonitorStateException  if the current thread is not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *               the owner of this object's monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @see        java.lang.Object#notifyAll()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @see        java.lang.Object#wait()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
31908
73eefd070da4 8075171: Contended Locking fast notify bucket
dcubed
parents: 31671
diff changeset
   281
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public final native void notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Wakes up all threads that are waiting on this object's monitor. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * thread waits on an object's monitor by calling one of the
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   287
     * {@code wait} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * The awakened threads will not be able to proceed until the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * thread relinquishes the lock on this object. The awakened threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * will compete in the usual manner with any other threads that might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * be actively competing to synchronize on this object; for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * the awakened threads enjoy no reliable privilege or disadvantage in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * being the next thread to lock this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * This method should only be called by a thread that is the owner
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   297
     * of this object's monitor. See the {@code notify} method for a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * description of the ways in which a thread can become the owner of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * a monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
11516
0e4924734eed 7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents: 9266
diff changeset
   301
     * @throws  IllegalMonitorStateException  if the current thread is not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *               the owner of this object's monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @see        java.lang.Object#notify()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @see        java.lang.Object#wait()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
31908
73eefd070da4 8075171: Contended Locking fast notify bucket
dcubed
parents: 31671
diff changeset
   306
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public final native void notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   310
     * Causes the current thread to wait until it is awakened, typically
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   311
     * by being <em>notified</em> or <em>interrupted</em>.
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   312
     * <p>
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   313
     * In all respects, this method behaves as if {@code wait(0L, 0)}
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   314
     * had been called. See the specification of the {@link #wait(long, int)} method
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   315
     * for details.
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   316
     *
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   317
     * @throws IllegalMonitorStateException if the current thread is not
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   318
     *         the owner of the object's monitor
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   319
     * @throws InterruptedException if any thread interrupted the current thread before or
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   320
     *         while the current thread was waiting. The <em>interrupted status</em> of the
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   321
     *         current thread is cleared when this exception is thrown.
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   322
     * @see    #notify()
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   323
     * @see    #notifyAll()
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   324
     * @see    #wait(long)
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   325
     * @see    #wait(long, int)
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   326
     */
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   327
    public final void wait() throws InterruptedException {
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   328
        wait(0L);
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   329
    }
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   330
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   331
    /**
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   332
     * Causes the current thread to wait until it is awakened, typically
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   333
     * by being <em>notified</em> or <em>interrupted</em>, or until a
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   334
     * certain amount of real time has elapsed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <p>
50474
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   336
     * In all respects, this method behaves as if {@code wait(timeoutMillis, 0)}
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   337
     * had been called. See the specification of the {@link #wait(long, int)} method
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   338
     * for details.
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   339
     *
50474
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   340
     * @param  timeoutMillis the maximum time to wait, in milliseconds
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   341
     * @throws IllegalArgumentException if {@code timeoutMillis} is negative
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   342
     * @throws IllegalMonitorStateException if the current thread is not
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   343
     *         the owner of the object's monitor
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   344
     * @throws InterruptedException if any thread interrupted the current thread before or
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   345
     *         while the current thread was waiting. The <em>interrupted status</em> of the
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   346
     *         current thread is cleared when this exception is thrown.
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   347
     * @see    #notify()
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   348
     * @see    #notifyAll()
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   349
     * @see    #wait()
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   350
     * @see    #wait(long, int)
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   351
     */
50474
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   352
    public final native void wait(long timeoutMillis) throws InterruptedException;
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   353
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   354
    /**
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   355
     * Causes the current thread to wait until it is awakened, typically
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   356
     * by being <em>notified</em> or <em>interrupted</em>, or until a
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   357
     * certain amount of real time has elapsed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <p>
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   359
     * The current thread must own this object's monitor lock. See the
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   360
     * {@link #notify notify} method for a description of the ways in which
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   361
     * a thread can become the owner of a monitor lock.
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   362
     * <p>
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   363
     * This method causes the current thread (referred to here as <var>T</var>) to
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   364
     * place itself in the wait set for this object and then to relinquish any
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   365
     * and all synchronization claims on this object. Note that only the locks
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   366
     * on this object are relinquished; any other objects on which the current
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   367
     * thread may be synchronized remain locked while the thread waits.
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   368
     * <p>
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   369
     * Thread <var>T</var> then becomes disabled for thread scheduling purposes
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   370
     * and lies dormant until one of the following occurs:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * <ul>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   372
     * <li>Some other thread invokes the {@code notify} method for this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * object and thread <var>T</var> happens to be arbitrarily chosen as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * the thread to be awakened.
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   375
     * <li>Some other thread invokes the {@code notifyAll} method for this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <li>Some other thread {@linkplain Thread#interrupt() interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * thread <var>T</var>.
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   379
     * <li>The specified amount of real time has elapsed, more or less.
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   380
     * The amount of real time, in nanoseconds, is given by the expression
50474
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   381
     * {@code 1000000 * timeoutMillis + nanos}. If {@code timeoutMillis} and {@code nanos}
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   382
     * are both zero, then real time is not taken into consideration and the
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   383
     * thread waits until awakened by one of the other causes.
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   384
     * <li>Thread <var>T</var> is awakened spuriously. (See below.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * </ul>
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   386
     * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * The thread <var>T</var> is then removed from the wait set for this
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   388
     * object and re-enabled for thread scheduling. It competes in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * usual manner with other threads for the right to synchronize on the
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   390
     * object; once it has regained control of the object, all its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * synchronization claims on the object are restored to the status quo
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   392
     * ante - that is, to the situation as of the time that the {@code wait}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * method was invoked. Thread <var>T</var> then returns from the
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   394
     * invocation of the {@code wait} method. Thus, on return from the
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   395
     * {@code wait} method, the synchronization state of the object and of
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   396
     * thread {@code T} is exactly as it was when the {@code wait} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * was invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <p>
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   399
     * A thread can wake up without being notified, interrupted, or timing out, a
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   400
     * so-called <em>spurious wakeup</em>.  While this will rarely occur in practice,
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   401
     * applications must guard against it by testing for the condition that should
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   402
     * have caused the thread to be awakened, and continuing to wait if the condition
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   403
     * is not satisfied. See the example below.
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   404
     * <p>
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   405
     * For more information on this topic, see section 14.2,
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   406
     * "Condition Queues," in Brian Goetz and others' <em>Java Concurrency
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   407
     * in Practice</em> (Addison-Wesley, 2006) or Item 69 in Joshua
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   408
     * Bloch's <em>Effective Java, Second Edition</em> (Addison-Wesley,
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   409
     * 2008).
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   410
     * <p>
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   411
     * If the current thread is {@linkplain java.lang.Thread#interrupt() interrupted}
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   412
     * by any thread before or while it is waiting, then an {@code InterruptedException}
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   413
     * is thrown.  The <em>interrupted status</em> of the current thread is cleared when
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   414
     * this exception is thrown. This exception is not thrown until the lock status of
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   415
     * this object has been restored as described above.
28683
a36c179aa292 8071434: doc updates for java.lang.Object
darcy
parents: 27733
diff changeset
   416
     *
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   417
     * @apiNote
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   418
     * The recommended approach to waiting is to check the condition being awaited in
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   419
     * a {@code while} loop around the call to {@code wait}, as shown in the example
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   420
     * below. Among other things, this approach avoids problems that can be caused
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   421
     * by spurious wakeups.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   423
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *     synchronized (obj) {
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   425
     *         while (<condition does not hold> and <timeout not exceeded>) {
50474
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   426
     *             long timeoutMillis = ... ; // recompute timeout values
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   427
     *             int nanos = ... ;
50474
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   428
     *             obj.wait(timeoutMillis, nanos);
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   429
     *         }
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   430
     *         ... // Perform action appropriate to condition or timeout
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *     }
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   432
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
50474
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   434
     * @param  timeoutMillis the maximum time to wait, in milliseconds
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   435
     * @param  nanos   additional time, in nanoseconds, in the range range 0-999999 inclusive
50474
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   436
     * @throws IllegalArgumentException if {@code timeoutMillis} is negative,
47333
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   437
     *         or if the value of {@code nanos} is out of range
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   438
     * @throws IllegalMonitorStateException if the current thread is not
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   439
     *         the owner of the object's monitor
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   440
     * @throws InterruptedException if any thread interrupted the current thread before or
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   441
     *         while the current thread was waiting. The <em>interrupted status</em> of the
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   442
     *         current thread is cleared when this exception is thrown.
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   443
     * @see    #notify()
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   444
     * @see    #notifyAll()
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   445
     * @see    #wait()
0c2e5ef6a1c6 6344935: (spec) clarify specifications for Object.wait overloads
smarks
parents: 47216
diff changeset
   446
     * @see    #wait(long)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
50474
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   448
    public final void wait(long timeoutMillis, int nanos) throws InterruptedException {
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   449
        if (timeoutMillis < 0) {
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   450
            throw new IllegalArgumentException("timeoutMillis value is negative");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (nanos < 0 || nanos > 999999) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                                "nanosecond timeout value out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
51758
6c956c883137 8210787: Object.wait(long, int) throws inappropriate IllegalArgumentException
igerasim
parents: 50474
diff changeset
   458
        if (nanos > 0 && timeoutMillis < Long.MAX_VALUE) {
50474
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   459
            timeoutMillis++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
50474
5d905cc4c358 8204377: Rename Object#wait parameter name from "timeout" to "timeoutMillis"
martin
parents: 47333
diff changeset
   462
        wait(timeoutMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Called by the garbage collector on an object when garbage collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * determines that there are no more references to the object.
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   468
     * A subclass overrides the {@code finalize} method to dispose of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * system resources or to perform other cleanup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * <p>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   471
     * The general contract of {@code finalize} is that it is invoked
11516
0e4924734eed 7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents: 9266
diff changeset
   472
     * if and when the Java&trade; virtual
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * machine has determined that there is no longer any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * means by which this object can be accessed by any thread that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * not yet died, except as a result of an action taken by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * finalization of some other object or class which is ready to be
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   477
     * finalized. The {@code finalize} method may take any action, including
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * making this object available again to other threads; the usual purpose
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   479
     * of {@code finalize}, however, is to perform cleanup actions before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * the object is irrevocably discarded. For example, the finalize method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * for an object that represents an input/output connection might perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * explicit I/O transactions to break the connection before the object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * permanently discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <p>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   485
     * The {@code finalize} method of class {@code Object} performs no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * special action; it simply returns normally. Subclasses of
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   487
     * {@code Object} may override this definition.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * The Java programming language does not guarantee which thread will
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   490
     * invoke the {@code finalize} method for any given object. It is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * guaranteed, however, that the thread that invokes finalize will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * be holding any user-visible synchronization locks when finalize is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * invoked. If an uncaught exception is thrown by the finalize method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * the exception is ignored and finalization of that object terminates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * <p>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   496
     * After the {@code finalize} method has been invoked for an object, no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * further action is taken until the Java virtual machine has again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * determined that there is no longer any means by which this object can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * be accessed by any thread that has not yet died, including possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * actions by other objects or classes which are ready to be finalized,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * at which point the object may be discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * <p>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   503
     * The {@code finalize} method is never invoked more than once by a Java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * virtual machine for any given object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * <p>
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   506
     * Any exception thrown by the {@code finalize} method causes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * the finalization of this object to be halted, but is otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   510
     * @apiNote
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   511
     * Classes that embed non-heap resources have many options
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   512
     * for cleanup of those resources. The class must ensure that the
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   513
     * lifetime of each instance is longer than that of any resource it embeds.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   514
     * {@link java.lang.ref.Reference#reachabilityFence} can be used to ensure that
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   515
     * objects remain reachable while resources embedded in the object are in use.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   516
     * <p>
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   517
     * A subclass should avoid overriding the {@code finalize} method
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   518
     * unless the subclass embeds non-heap resources that must be cleaned up
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   519
     * before the instance is collected.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   520
     * Finalizer invocations are not automatically chained, unlike constructors.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   521
     * If a subclass overrides {@code finalize} it must invoke the superclass
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   522
     * finalizer explicitly.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   523
     * To guard against exceptions prematurely terminating the finalize chain,
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   524
     * the subclass should use a {@code try-finally} block to ensure
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   525
     * {@code super.finalize()} is always invoked. For example,
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   526
     * <pre>{@code      @Override
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   527
     *     protected void finalize() throws Throwable {
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   528
     *         try {
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   529
     *             ... // cleanup subclass state
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   530
     *         } finally {
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   531
     *             super.finalize();
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   532
     *         }
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   533
     *     }
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   534
     * }</pre>
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   535
     *
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   536
     * @deprecated The finalization mechanism is inherently problematic.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   537
     * Finalization can lead to performance issues, deadlocks, and hangs.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   538
     * Errors in finalizers can lead to resource leaks; there is no way to cancel
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   539
     * finalization if it is no longer necessary; and no ordering is specified
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   540
     * among calls to {@code finalize} methods of different objects.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   541
     * Furthermore, there are no guarantees regarding the timing of finalization.
44540
dbc181a6e4a7 8178154: Typo in Object.finalize deprecation javadoc
rriggs
parents: 44534
diff changeset
   542
     * The {@code finalize} method might be called on a finalizable object
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   543
     * only after an indefinite delay, if at all.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   544
     *
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   545
     * Classes whose instances hold non-heap resources should provide a method
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   546
     * to enable explicit release of those resources, and they should also
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   547
     * implement {@link AutoCloseable} if appropriate.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   548
     * The {@link java.lang.ref.Cleaner} and {@link java.lang.ref.PhantomReference}
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   549
     * provide more flexible and efficient ways to release resources when an object
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   550
     * becomes unreachable.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   551
     *
1937
28c31d4a9597 6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents: 2
diff changeset
   552
     * @throws Throwable the {@code Exception} raised by this method
11516
0e4924734eed 7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents: 9266
diff changeset
   553
     * @see java.lang.ref.WeakReference
0e4924734eed 7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents: 9266
diff changeset
   554
     * @see java.lang.ref.PhantomReference
0e4924734eed 7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents: 9266
diff changeset
   555
     * @jls 12.6 Finalization of Class Instances
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     */
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 31908
diff changeset
   557
    @Deprecated(since="9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    protected void finalize() throws Throwable { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
}