📄 object.java
字号:
* <li><code>o.equals(o)</code> is true</li> * </ul> * * <p>However, these are not strict requirements, and may * be violated if necessary. Of the three requirements, the * last is the most commonly violated, particularly if the * subclass does not override {@link #equals(Object)}. * * <p>If the Object you call clone() on does not implement * {@link Cloneable} (which is a placeholder interface), then * a CloneNotSupportedException is thrown. Notice that * Object does not implement Cloneable; this method exists * as a convenience for subclasses that do. * * <p>Object's implementation of clone allocates space for the * new Object using the correct class, without calling any * constructors, and then fills in all of the new field values * with the old field values. Thus, it is a shallow copy. * However, subclasses are permitted to make a deep copy. * * <p>All array types implement Cloneable, and override * this method as follows (it should never fail):<br> * <pre> * public Object clone() * { * try * { * super.clone(); * } * catch (CloneNotSupportedException e) * { * throw new InternalError(e.getMessage()); * } * } * </pre> * * @return a copy of the Object * @throws CloneNotSupportedException If this Object does not * implement Cloneable * @throws OutOfMemoryError Since cloning involves memory allocation, * even though it may bypass constructors, you might run * out of memory * @see Cloneable */ protected Object clone() throws CloneNotSupportedException { if (this instanceof Cloneable) return VMObject.clone((Cloneable) this); throw new CloneNotSupportedException("Object not cloneable"); } /** * Returns the runtime {@link Class} of this Object. * * <p>The class object can also be obtained without a runtime * instance by using the class literal, as in: * <code>Foo.class</code>. Notice that the class literal * also works on primitive types, making it useful for * reflection purposes. * * @return the class of this Object */ public final Class getClass() { return VMObject.getClass(this); } /** * Wakes up one of the {@link Thread}s that has called * <code>wait</code> on this Object. Only the owner * of a lock on this Object may call this method. This lock * is obtained by a <code>synchronized</code> method or statement. * * <p>The Thread to wake up is chosen arbitrarily. The * awakened thread is not guaranteed to be the next thread * to actually obtain the lock on this object. * * <p>This thread still holds a lock on the object, so it is * typical to release the lock by exiting the synchronized * code, calling wait(), or calling {@link Thread#sleep(long)}, so * that the newly awakened thread can actually resume. The * awakened thread will most likely be awakened with an * {@link InterruptedException}, but that is not guaranteed. * * @throws IllegalMonitorStateException if this Thread * does not own the lock on the Object * @see #notifyAll() * @see #wait() * @see #wait(long) * @see #wait(long, int) * @see Thread */ public final void notify() throws IllegalMonitorStateException { VMObject.notify(this); } /** * Wakes up all of the {@link Thread}s that have called * <code>wait</code> on this Object. Only the owner * of a lock on this Object may call this method. This lock * is obtained by a <code>synchronized</code> method or statement. * * <p>There are no guarantees as to which thread will next * obtain the lock on the object. * * <p>This thread still holds a lock on the object, so it is * typical to release the lock by exiting the synchronized * code, calling wait(), or calling {@link Thread#sleep(long)}, so * that one of the newly awakened threads can actually resume. * The resuming thread will most likely be awakened with an * {@link InterruptedException}, but that is not guaranteed. * * @throws IllegalMonitorStateException if this Thread * does not own the lock on the Object * @see #notify() * @see #wait() * @see #wait(long) * @see #wait(long, int) * @see Thread */ public final void notifyAll() throws IllegalMonitorStateException { VMObject.notifyAll(this); } /** * Waits indefinitely for notify() or notifyAll() to be * called on the Object in question. Implementation is * identical to wait(0). * * <p>The Thread that calls wait must have a lock on this Object, * obtained by a <code>synchronized</code> method or statement. * After calling wait, the thread loses the lock on this * object until the method completes (abruptly or normally), * at which time it regains the lock. All locks held on * other objects remain in force, even though the thread is * inactive. Therefore, caution must be used to avoid deadlock. * * <p>While it is typical that this method will complete abruptly * with an {@link InterruptedException}, it is not guaranteed. So, * it is typical to call wait inside an infinite loop:<br> * * <pre> * try * { * while (true) * lock.wait(); * } * catch (InterruptedException e) * { * } * </pre> * * @throws IllegalMonitorStateException if this Thread * does not own a lock on this Object * @throws InterruptedException if some other Thread * interrupts this Thread * @see #notify() * @see #notifyAll() * @see #wait(long) * @see #wait(long, int) * @see Thread */ public final void wait() throws IllegalMonitorStateException, InterruptedException { VMObject.wait(this, 0, 0); } /** * Waits a specified amount of time (or indefinitely if * the time specified is 0) for someone to call notify() * or notifyAll() on this Object, waking up this Thread. * * <p>The Thread that calls wait must have a lock on this Object, * obtained by a <code>synchronized</code> method or statement. * After calling wait, the thread loses the lock on this * object until the method completes (abruptly or normally), * at which time it regains the lock. All locks held on * other objects remain in force, even though the thread is * inactive. Therefore, caution must be used to avoid deadlock. * * <p>Usually, this call will complete normally if the time * expires, or abruptly with {@link InterruptedException} * if another thread called notify, but neither result * is guaranteed. * * <p>The waiting period is only *roughly* the amount of time * you requested. It cannot be exact because of the overhead * of the call itself. Most Virtual Machiness treat the * argument as a lower limit on the time spent waiting, but * even that is not guaranteed. Besides, some other thread * may hold the lock on the object when the time expires, so * the current thread may still have to wait to reobtain the * lock. * * @param ms the minimum number of milliseconds to wait (1000 * milliseconds = 1 second), or 0 for an indefinite wait * @throws IllegalArgumentException if ms < 0 * @throws IllegalMonitorStateException if this Thread * does not own a lock on this Object * @throws InterruptedException if some other Thread * interrupts this Thread * @see #notify() * @see #notifyAll() * @see #wait() * @see #wait(long, int) * @see Thread */ public final void wait(long ms) throws IllegalMonitorStateException, InterruptedException { wait(ms, 0); } /** * Waits a specified amount of time (or indefinitely if * the time specified is 0) for someone to call notify() * or notifyAll() on this Object, waking up this Thread. * * <p>The Thread that calls wait must have a lock on this Object, * obtained by a <code>synchronized</code> method or statement. * After calling wait, the thread loses the lock on this * object until the method completes (abruptly or normally), * at which time it regains the lock. All locks held on * other objects remain in force, even though the thread is * inactive. Therefore, caution must be used to avoid deadlock. * * <p>Usually, this call will complete normally if the time * expires, or abruptly with {@link InterruptedException} * if another thread called notify, but neither result * is guaranteed. * * <p>The waiting period is nowhere near as precise as * nanoseconds; considering that even wait(int) is inaccurate, * how much can you expect? But on supporting * implementations, this offers somewhat more granularity * than milliseconds. * * @param ms the number of milliseconds to wait (1,000 * milliseconds = 1 second) * @param ns the number of nanoseconds to wait over and * above ms (1,000,000 nanoseconds = 1 millisecond) * @throws IllegalArgumentException if ms < 0 or ns is not * in the range 0 to 999,999 * @throws IllegalMonitorStateException if this Thread * does not own a lock on this Object * @throws InterruptedException if some other Thread * interrupts this Thread * @see #notify() * @see #notifyAll() * @see #wait() * @see #wait(long) * @see Thread */ public final void wait(long ms, int ns) throws IllegalMonitorStateException, InterruptedException { if (ms < 0 || ns < 0 || ns > 999999) throw new IllegalArgumentException("argument out of range"); VMObject.wait(this, ms, ns); }} // class Object
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -