author | alanb |
Thu, 01 Dec 2016 08:57:53 +0000 | |
changeset 42338 | a60f280f803c |
parent 31908 | 73eefd070da4 |
child 44534 | a076dffbc2c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
28683 | 2 |
* Copyright (c) 1994, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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 |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
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 | 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 | 33 |
* including arrays, implement the methods of this class. |
34 |
* |
|
35 |
* @author unascribed |
|
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 | 38 |
*/ |
39 |
public class Object { |
|
40 |
||
41 |
private static native void registerNatives(); |
|
42 |
static { |
|
43 |
registerNatives(); |
|
44 |
} |
|
45 |
||
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 | 53 |
* Returns the runtime class of this {@code Object}. The returned |
54 |
* {@code Class} object is the object that is locked by {@code |
|
55 |
* static synchronized} methods of the represented class. |
|
56 |
* |
|
57 |
* <p><b>The actual result type is {@code Class<? extends |X|>} |
|
58 |
* where {@code |X|} is the erasure of the static type of the |
|
59 |
* expression on which {@code getClass} is called.</b> For |
|
60 |
* example, no cast is required in this code fragment:</p> |
|
61 |
* |
|
62 |
* <p> |
|
63 |
* {@code Number n = 0; }<br> |
|
64 |
* {@code Class<? extends Number> c = n.getClass(); } |
|
65 |
* </p> |
|
66 |
* |
|
67 |
* @return The {@code Class} object that represents the runtime |
|
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 | 70 |
*/ |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28685
diff
changeset
|
71 |
@HotSpotIntrinsicCandidate |
2 | 72 |
public final native Class<?> getClass(); |
73 |
||
74 |
/** |
|
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 | 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 | 80 |
* <ul> |
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 | 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 | 85 |
* This integer need not remain consistent from one execution of an |
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 | 89 |
* the two objects must produce the same integer result. |
90 |
* <li>It is <em>not</em> required that if two objects are unequal |
|
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 | 93 |
* two objects must produce distinct integer results. However, the |
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 | 96 |
* </ul> |
97 |
* <p> |
|
28683 | 98 |
* As much as is reasonably practical, the hashCode method defined |
99 |
* by class {@code Object} does return distinct integers for |
|
100 |
* distinct objects. (The hashCode may or may not be implemented |
|
101 |
* as some function of an object's memory address at some point |
|
102 |
* in time.) |
|
2 | 103 |
* |
104 |
* @return a hash code value for this object. |
|
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 | 107 |
*/ |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28685
diff
changeset
|
108 |
@HotSpotIntrinsicCandidate |
2 | 109 |
public native int hashCode(); |
110 |
||
111 |
/** |
|
112 |
* Indicates whether some other object is "equal to" this one. |
|
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 | 115 |
* on non-null object references: |
116 |
* <ul> |
|
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 | 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 | 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 | 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 | 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 | 137 |
* </ul> |
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 | 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 | 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 | 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 | 149 |
* that equal objects must have equal hash codes. |
150 |
* |
|
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 | 154 |
* @see #hashCode() |
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
155 |
* @see java.util.HashMap |
2 | 156 |
*/ |
157 |
public boolean equals(Object obj) { |
|
158 |
return (this == obj); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Creates and returns a copy of this object. The precise meaning |
|
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 | 165 |
* <blockquote> |
166 |
* <pre> |
|
167 |
* x.clone() != x</pre></blockquote> |
|
168 |
* will be true, and that the expression: |
|
169 |
* <blockquote> |
|
170 |
* <pre> |
|
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 | 173 |
* While it is typically the case that: |
174 |
* <blockquote> |
|
175 |
* <pre> |
|
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 | 178 |
* <p> |
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 | 183 |
* <p> |
184 |
* By convention, the object returned by this method should be independent |
|
185 |
* of this object (which is being cloned). To achieve this independence, |
|
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 | 188 |
* copying any mutable objects that comprise the internal "deep structure" |
189 |
* of the object being cloned and replacing the references to these |
|
190 |
* objects with references to the copies. If a class contains only |
|
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 | 193 |
* need to be modified. |
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 | 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 | 199 |
* are considered to implement the interface {@code Cloneable} and that |
200 |
* the return type of the {@code clone} method of an array type {@code T[]} |
|
201 |
* is {@code T[]} where T is any reference or primitive type. |
|
2 | 202 |
* Otherwise, this method creates a new instance of the class of this |
203 |
* object and initializes all its fields with exactly the contents of |
|
204 |
* the corresponding fields of this object, as if by assignment; the |
|
205 |
* contents of the fields are not themselves cloned. Thus, this method |
|
206 |
* performs a "shallow copy" of this object, not a "deep copy" operation. |
|
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 | 211 |
* exception at run time. |
212 |
* |
|
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 | 217 |
* throw this exception to indicate that an instance cannot |
218 |
* be cloned. |
|
219 |
* @see java.lang.Cloneable |
|
220 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
28685
diff
changeset
|
221 |
@HotSpotIntrinsicCandidate |
2 | 222 |
protected native Object clone() throws CloneNotSupportedException; |
223 |
||
224 |
/** |
|
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 | 227 |
* "textually represents" this object. The result should |
228 |
* be a concise but informative representation that is easy for a |
|
229 |
* person to read. |
|
230 |
* It is recommended that all subclasses override this method. |
|
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 | 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 | 235 |
* the unsigned hexadecimal representation of the hash code of the |
236 |
* object. In other words, this method returns a string equal to the |
|
237 |
* value of: |
|
238 |
* <blockquote> |
|
239 |
* <pre> |
|
240 |
* getClass().getName() + '@' + Integer.toHexString(hashCode()) |
|
241 |
* </pre></blockquote> |
|
242 |
* |
|
243 |
* @return a string representation of the object. |
|
244 |
*/ |
|
245 |
public String toString() { |
|
246 |
return getClass().getName() + "@" + Integer.toHexString(hashCode()); |
|
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* Wakes up a single thread that is waiting on this object's |
|
251 |
* monitor. If any threads are waiting on this object, one of them |
|
252 |
* is chosen to be awakened. The choice is arbitrary and occurs at |
|
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 | 255 |
* <p> |
256 |
* The awakened thread will not be able to proceed until the current |
|
257 |
* thread relinquishes the lock on this object. The awakened thread will |
|
258 |
* compete in the usual manner with any other threads that might be |
|
259 |
* actively competing to synchronize on this object; for example, the |
|
260 |
* awakened thread enjoys no reliable privilege or disadvantage in being |
|
261 |
* the next thread to lock this object. |
|
262 |
* <p> |
|
263 |
* This method should only be called by a thread that is the owner |
|
264 |
* of this object's monitor. A thread becomes the owner of the |
|
265 |
* object's monitor in one of three ways: |
|
266 |
* <ul> |
|
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 | 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 | 271 |
* synchronized static method of that class. |
272 |
* </ul> |
|
273 |
* <p> |
|
274 |
* Only one thread at a time can own an object's monitor. |
|
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 | 277 |
* the owner of this object's monitor. |
278 |
* @see java.lang.Object#notifyAll() |
|
279 |
* @see java.lang.Object#wait() |
|
280 |
*/ |
|
31908 | 281 |
@HotSpotIntrinsicCandidate |
2 | 282 |
public final native void notify(); |
283 |
||
284 |
/** |
|
285 |
* Wakes up all threads that are waiting on this object's monitor. A |
|
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 | 288 |
* <p> |
289 |
* The awakened threads will not be able to proceed until the current |
|
290 |
* thread relinquishes the lock on this object. The awakened threads |
|
291 |
* will compete in the usual manner with any other threads that might |
|
292 |
* be actively competing to synchronize on this object; for example, |
|
293 |
* the awakened threads enjoy no reliable privilege or disadvantage in |
|
294 |
* being the next thread to lock this object. |
|
295 |
* <p> |
|
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 | 298 |
* description of the ways in which a thread can become the owner of |
299 |
* a monitor. |
|
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 | 302 |
* the owner of this object's monitor. |
303 |
* @see java.lang.Object#notify() |
|
304 |
* @see java.lang.Object#wait() |
|
305 |
*/ |
|
31908 | 306 |
@HotSpotIntrinsicCandidate |
2 | 307 |
public final native void notifyAll(); |
308 |
||
309 |
/** |
|
310 |
* Causes the current thread to wait until either another thread invokes the |
|
311 |
* {@link java.lang.Object#notify()} method or the |
|
312 |
* {@link java.lang.Object#notifyAll()} method for this object, or a |
|
313 |
* specified amount of time has elapsed. |
|
314 |
* <p> |
|
315 |
* The current thread must own this object's monitor. |
|
316 |
* <p> |
|
317 |
* This method causes the current thread (call it <var>T</var>) to |
|
318 |
* place itself in the wait set for this object and then to relinquish |
|
319 |
* any and all synchronization claims on this object. Thread <var>T</var> |
|
320 |
* becomes disabled for thread scheduling purposes and lies dormant |
|
321 |
* until one of four things happens: |
|
322 |
* <ul> |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
323 |
* <li>Some other thread invokes the {@code notify} method for this |
2 | 324 |
* object and thread <var>T</var> happens to be arbitrarily chosen as |
325 |
* the thread to be awakened. |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
326 |
* <li>Some other thread invokes the {@code notifyAll} method for this |
2 | 327 |
* object. |
328 |
* <li>Some other thread {@linkplain Thread#interrupt() interrupts} |
|
329 |
* thread <var>T</var>. |
|
330 |
* <li>The specified amount of real time has elapsed, more or less. If |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
331 |
* {@code timeout} is zero, however, then real time is not taken into |
2 | 332 |
* consideration and the thread simply waits until notified. |
333 |
* </ul> |
|
334 |
* The thread <var>T</var> is then removed from the wait set for this |
|
335 |
* object and re-enabled for thread scheduling. It then competes in the |
|
336 |
* usual manner with other threads for the right to synchronize on the |
|
337 |
* object; once it has gained control of the object, all its |
|
338 |
* 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
|
339 |
* ante - that is, to the situation as of the time that the {@code wait} |
2 | 340 |
* 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
|
341 |
* invocation of the {@code wait} method. Thus, on return from the |
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
342 |
* {@code wait} method, the synchronization state of the object and of |
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
343 |
* thread {@code T} is exactly as it was when the {@code wait} method |
2 | 344 |
* was invoked. |
345 |
* <p> |
|
346 |
* A thread can also wake up without being notified, interrupted, or |
|
347 |
* timing out, a so-called <i>spurious wakeup</i>. While this will rarely |
|
348 |
* occur in practice, applications must guard against it by testing for |
|
349 |
* the condition that should have caused the thread to be awakened, and |
|
350 |
* continuing to wait if the condition is not satisfied. In other words, |
|
351 |
* waits should always occur in loops, like this one: |
|
352 |
* <pre> |
|
353 |
* synchronized (obj) { |
|
354 |
* while (<condition does not hold>) |
|
355 |
* obj.wait(timeout); |
|
356 |
* ... // Perform action appropriate to condition |
|
357 |
* } |
|
358 |
* </pre> |
|
28683 | 359 |
* |
360 |
* (For more information on this topic, see section 14.2, |
|
361 |
* Condition Queues, in Brian Goetz and others' "Java Concurrency |
|
362 |
* in Practice" (Addison-Wesley, 2006) or Item 69 in Joshua |
|
363 |
* Bloch's "Effective Java (Second Edition)" (Addison-Wesley, |
|
364 |
* 2008). |
|
2 | 365 |
* |
366 |
* <p>If the current thread is {@linkplain java.lang.Thread#interrupt() |
|
367 |
* interrupted} by any thread before or while it is waiting, then an |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
368 |
* {@code InterruptedException} is thrown. This exception is not |
2 | 369 |
* thrown until the lock status of this object has been restored as |
370 |
* described above. |
|
371 |
* |
|
372 |
* <p> |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
373 |
* Note that the {@code wait} method, as it places the current thread |
2 | 374 |
* into the wait set for this object, unlocks only this object; any |
375 |
* other objects on which the current thread may be synchronized remain |
|
376 |
* locked while the thread waits. |
|
377 |
* <p> |
|
378 |
* 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
|
379 |
* of this object's monitor. See the {@code notify} method for a |
2 | 380 |
* description of the ways in which a thread can become the owner of |
381 |
* a monitor. |
|
382 |
* |
|
383 |
* @param timeout the maximum time to wait in milliseconds. |
|
11516
0e4924734eed
7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents:
9266
diff
changeset
|
384 |
* @throws IllegalArgumentException if the value of timeout is |
2 | 385 |
* negative. |
11516
0e4924734eed
7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents:
9266
diff
changeset
|
386 |
* @throws IllegalMonitorStateException if the current thread is not |
2 | 387 |
* the owner of the object's monitor. |
11516
0e4924734eed
7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents:
9266
diff
changeset
|
388 |
* @throws InterruptedException if any thread interrupted the |
2 | 389 |
* current thread before or while the current thread |
390 |
* was waiting for a notification. The <i>interrupted |
|
391 |
* status</i> of the current thread is cleared when |
|
392 |
* this exception is thrown. |
|
393 |
* @see java.lang.Object#notify() |
|
394 |
* @see java.lang.Object#notifyAll() |
|
395 |
*/ |
|
396 |
public final native void wait(long timeout) throws InterruptedException; |
|
397 |
||
398 |
/** |
|
399 |
* Causes the current thread to wait until another thread invokes the |
|
400 |
* {@link java.lang.Object#notify()} method or the |
|
401 |
* {@link java.lang.Object#notifyAll()} method for this object, or |
|
402 |
* some other thread interrupts the current thread, or a certain |
|
403 |
* amount of real time has elapsed. |
|
404 |
* <p> |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
405 |
* This method is similar to the {@code wait} method of one |
2 | 406 |
* argument, but it allows finer control over the amount of time to |
407 |
* wait for a notification before giving up. The amount of real time, |
|
408 |
* measured in nanoseconds, is given by: |
|
409 |
* <blockquote> |
|
410 |
* <pre> |
|
411 |
* 1000000*timeout+nanos</pre></blockquote> |
|
412 |
* <p> |
|
413 |
* In all other respects, this method does the same thing as the |
|
414 |
* method {@link #wait(long)} of one argument. In particular, |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
415 |
* {@code wait(0, 0)} means the same thing as {@code wait(0)}. |
2 | 416 |
* <p> |
417 |
* The current thread must own this object's monitor. The thread |
|
418 |
* releases ownership of this monitor and waits until either of the |
|
419 |
* following two conditions has occurred: |
|
420 |
* <ul> |
|
421 |
* <li>Another thread notifies threads waiting on this object's monitor |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
422 |
* to wake up either through a call to the {@code notify} method |
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
423 |
* or the {@code notifyAll} method. |
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
424 |
* <li>The timeout period, specified by {@code timeout} |
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
425 |
* milliseconds plus {@code nanos} nanoseconds arguments, has |
2 | 426 |
* elapsed. |
427 |
* </ul> |
|
428 |
* <p> |
|
429 |
* The thread then waits until it can re-obtain ownership of the |
|
430 |
* monitor and resumes execution. |
|
431 |
* <p> |
|
432 |
* As in the one argument version, interrupts and spurious wakeups are |
|
433 |
* possible, and this method should always be used in a loop: |
|
434 |
* <pre> |
|
435 |
* synchronized (obj) { |
|
436 |
* while (<condition does not hold>) |
|
437 |
* obj.wait(timeout, nanos); |
|
438 |
* ... // Perform action appropriate to condition |
|
439 |
* } |
|
440 |
* </pre> |
|
441 |
* 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
|
442 |
* of this object's monitor. See the {@code notify} method for a |
2 | 443 |
* description of the ways in which a thread can become the owner of |
444 |
* a monitor. |
|
445 |
* |
|
446 |
* @param timeout the maximum time to wait in milliseconds. |
|
447 |
* @param nanos additional time, in nanoseconds range |
|
448 |
* 0-999999. |
|
11516
0e4924734eed
7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents:
9266
diff
changeset
|
449 |
* @throws IllegalArgumentException if the value of timeout is |
2 | 450 |
* negative or the value of nanos is |
451 |
* not in the range 0-999999. |
|
11516
0e4924734eed
7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents:
9266
diff
changeset
|
452 |
* @throws IllegalMonitorStateException if the current thread is not |
2 | 453 |
* the owner of this object's monitor. |
11516
0e4924734eed
7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents:
9266
diff
changeset
|
454 |
* @throws InterruptedException if any thread interrupted the |
2 | 455 |
* current thread before or while the current thread |
456 |
* was waiting for a notification. The <i>interrupted |
|
457 |
* status</i> of the current thread is cleared when |
|
458 |
* this exception is thrown. |
|
459 |
*/ |
|
460 |
public final void wait(long timeout, int nanos) throws InterruptedException { |
|
461 |
if (timeout < 0) { |
|
462 |
throw new IllegalArgumentException("timeout value is negative"); |
|
463 |
} |
|
464 |
||
465 |
if (nanos < 0 || nanos > 999999) { |
|
466 |
throw new IllegalArgumentException( |
|
467 |
"nanosecond timeout value out of range"); |
|
468 |
} |
|
469 |
||
27733
e4e4d12307d7
8065372: Object.wait(ms, ns) timeout returns early
rriggs
parents:
25859
diff
changeset
|
470 |
if (nanos > 0) { |
2 | 471 |
timeout++; |
472 |
} |
|
473 |
||
474 |
wait(timeout); |
|
475 |
} |
|
476 |
||
477 |
/** |
|
478 |
* Causes the current thread to wait until another thread invokes the |
|
479 |
* {@link java.lang.Object#notify()} method or the |
|
480 |
* {@link java.lang.Object#notifyAll()} method for this object. |
|
481 |
* In other words, this method behaves exactly as if it simply |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
482 |
* performs the call {@code wait(0)}. |
2 | 483 |
* <p> |
484 |
* The current thread must own this object's monitor. The thread |
|
485 |
* releases ownership of this monitor and waits until another thread |
|
486 |
* notifies threads waiting on this object's monitor to wake up |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
487 |
* either through a call to the {@code notify} method or the |
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
488 |
* {@code notifyAll} method. The thread then waits until it can |
2 | 489 |
* re-obtain ownership of the monitor and resumes execution. |
490 |
* <p> |
|
491 |
* As in the one argument version, interrupts and spurious wakeups are |
|
492 |
* possible, and this method should always be used in a loop: |
|
493 |
* <pre> |
|
494 |
* synchronized (obj) { |
|
495 |
* while (<condition does not hold>) |
|
496 |
* obj.wait(); |
|
497 |
* ... // Perform action appropriate to condition |
|
498 |
* } |
|
499 |
* </pre> |
|
500 |
* 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
|
501 |
* of this object's monitor. See the {@code notify} method for a |
2 | 502 |
* description of the ways in which a thread can become the owner of |
503 |
* a monitor. |
|
504 |
* |
|
11516
0e4924734eed
7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents:
9266
diff
changeset
|
505 |
* @throws IllegalMonitorStateException if the current thread is not |
2 | 506 |
* the owner of the object's monitor. |
11516
0e4924734eed
7112008: Javadoc for j.l.Object.finalize() vs JLS 12.6 Finalization of Class Instances
darcy
parents:
9266
diff
changeset
|
507 |
* @throws InterruptedException if any thread interrupted the |
2 | 508 |
* current thread before or while the current thread |
509 |
* was waiting for a notification. The <i>interrupted |
|
510 |
* status</i> of the current thread is cleared when |
|
511 |
* this exception is thrown. |
|
512 |
* @see java.lang.Object#notify() |
|
513 |
* @see java.lang.Object#notifyAll() |
|
514 |
*/ |
|
515 |
public final void wait() throws InterruptedException { |
|
516 |
wait(0); |
|
517 |
} |
|
518 |
||
519 |
/** |
|
520 |
* Called by the garbage collector on an object when garbage collection |
|
521 |
* determines that there are no more references to the object. |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
522 |
* A subclass overrides the {@code finalize} method to dispose of |
2 | 523 |
* system resources or to perform other cleanup. |
524 |
* <p> |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
525 |
* 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
|
526 |
* if and when the Java™ virtual |
2 | 527 |
* machine has determined that there is no longer any |
528 |
* means by which this object can be accessed by any thread that has |
|
529 |
* not yet died, except as a result of an action taken by the |
|
530 |
* 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
|
531 |
* finalized. The {@code finalize} method may take any action, including |
2 | 532 |
* 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
|
533 |
* of {@code finalize}, however, is to perform cleanup actions before |
2 | 534 |
* the object is irrevocably discarded. For example, the finalize method |
535 |
* for an object that represents an input/output connection might perform |
|
536 |
* explicit I/O transactions to break the connection before the object is |
|
537 |
* permanently discarded. |
|
538 |
* <p> |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
539 |
* The {@code finalize} method of class {@code Object} performs no |
2 | 540 |
* special action; it simply returns normally. Subclasses of |
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
541 |
* {@code Object} may override this definition. |
2 | 542 |
* <p> |
543 |
* The Java programming language does not guarantee which thread will |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
544 |
* invoke the {@code finalize} method for any given object. It is |
2 | 545 |
* guaranteed, however, that the thread that invokes finalize will not |
546 |
* be holding any user-visible synchronization locks when finalize is |
|
547 |
* invoked. If an uncaught exception is thrown by the finalize method, |
|
548 |
* the exception is ignored and finalization of that object terminates. |
|
549 |
* <p> |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
550 |
* After the {@code finalize} method has been invoked for an object, no |
2 | 551 |
* further action is taken until the Java virtual machine has again |
552 |
* determined that there is no longer any means by which this object can |
|
553 |
* be accessed by any thread that has not yet died, including possible |
|
554 |
* actions by other objects or classes which are ready to be finalized, |
|
555 |
* at which point the object may be discarded. |
|
556 |
* <p> |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
557 |
* The {@code finalize} method is never invoked more than once by a Java |
2 | 558 |
* virtual machine for any given object. |
559 |
* <p> |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
560 |
* Any exception thrown by the {@code finalize} method causes |
2 | 561 |
* the finalization of this object to be halted, but is otherwise |
562 |
* ignored. |
|
563 |
* |
|
1937
28c31d4a9597
6239194: Object.hashCode() should reference System.identityHashCode()
darcy
parents:
2
diff
changeset
|
564 |
* @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
|
565 |
* @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
|
566 |
* @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
|
567 |
* @jls 12.6 Finalization of Class Instances |
2 | 568 |
*/ |
569 |
protected void finalize() throws Throwable { } |
|
570 |
} |