`
houjx
  • 浏览: 8037 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

java.lang.Object注

阅读更多
发现在这里显示的效果不是很好,所以发个我网易博客这篇文章的传送门:http://houjx114.blog.163.com/blog/static/10085942220111034121078/
“本文黑色字体为JavaSE源码注释,蓝色字体为API中文注释,红色字体为作者注释。由于作者水平有限,文中难免存在错误、疏漏之处,敬请广大访客留言批评指正。”
package java.lang;
Class Object is the root of the class hierarchy. Every class has Object as superclass. All objects, including arrays, implement the methods of this class.
类 Object 是类层次结构的根类。每个类都使用 Object 作为超类。所有对象(包括数组)都实现这个类的方法。

@author unascribed
@see   java.lang.Class
@since  JDK1.0

public class Object
{
registerNaives方法由Java虚拟机在创造对象时调用,其功能是初始化一个对象:分配内存空间,定义变量,产生this指针等等。
private static native void registerNatives();

静态代码块,当该类被实例化、类的静态方法被调用、类中的主方法被运行、该类被继承时都先执行该代码块
当该类被实例化时静态代码块优先于构造方法运行。
当该类被继承时静态代码块优先于子类的任何方法包括主方法
因此每一个对象继承object类时都会执行registerNatives方法以分配该对象的各种资源。
static
{
registerNatives();
}

Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.
返回此Object的运行时类。运行时类是指在程序运行时所确定的类型。一般而言运行时类与编译时类是相同的,但对象的类型有可能被强制转换,所以运行时类比编译时类能够更加准确的形容一个对象在某一时刻的类型。返回的Class对象是由所表示类的 static synchronized 方法(如果有这种修饰词的方法的话)锁定的对象。synchronized所修饰的块只同步锁定实例对象,在不同对象间可以异步,而static synchronized所修饰的块同步锁定该类的所用实例对象,该类的对象都将同步。
The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called. For example, no cast is required in this code fragment:
实际结果类型是Class<? extends |X|>,其中|X|表示清除表达式中的静态类型,该表达式调用 getClass。<? extends |X|>似乎是class类独用的泛型方式,经尝试将这种形式写在其他的地方都会出现语法错误。而具体何为“清除表达式中的静态类型”难以理解。到底表达式为何呢?如以下例子为例的话表达式是指n吗?希望有大虾能给予解答。例如,以下代码片段中不需要强制转换:
Number n = 0;
Class<? extends Number> c = n.getClass();

@return The Class object that represents the runtime class of this object.
表示此对象运行时类的 Class 对象。
@see   The Java Language Specification, Third Edition (15.8.2 Class Literals)

public final native Class<?> getClass();

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.
返回该对象的哈希码值。支持此方法是为了提高哈希表(例如 java.util.Hashtable 提供的哈希表)的性能。
在object类中,一般情况下不同的实例对象调用hashCode方法返回不用的哈希码值,但当对象数量太多时就会出现重复。所以不建议使用比较哈希码值的方法来判断实例对象是否同一。
The general contract of hashCode is:
hashCode 的常规协定是:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
在 Java 应用程序执行期间,在对同一对象多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是将对象进行 equals 比较时所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。
If two objects are equal according to the equals(Object)} method, then calling the hashCode} method on each of the two objects must produce the same integer result.
如果根据 equals(java.lang.Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果。
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.  However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么对这两个对象中的任一对象上调用 hashCode 方法不要求一定生成不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。
从某种层面上说这里要求了equals方法和hashCode方法在多态性上也要达到结果关联,也就是说程序员自定义的类如果要重写equals或hashCode方法的话也要符合当equals判断结果为true时每个对象的hashCode结果一致的要求。
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
实际上,由 Object 类定义的 hashCode 方法确实会针对不同的对象返回不同的整数。(这一般是通过将该对象的内部地址转换成一个整数来实现的,但是 JavaTM 编程语言不需要这种实现技巧。)
@return  a hash code value for this object.
此对象的一个哈希码值。
@see     equals(java.lang.Object)
@see     identityHashCode

public native int hashCode();

Indicates whether some other object is "equal to" this one.
指示其他某个对象是否与此对象“相等”。
比较的是实例对象而不是所存的值,更不是类。
The equals method implements an equivalence relation on non-null object references:
equals 方法在非空对象引用上实现相等关系:
这里的相等关系要符合离散数学相等的概念。
It is reflexive: for any non-null reference value x, x.equals(x) should return true.
自反性:对于任何非空引用值 x,x.equals(x) 都应返回 true。
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
对称性:对于任何非空引用值 x 和 y,当且仅当 y.equals(x) 返回 true 时,x.equals(y) 才应返回 true。
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
传递性:对于任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true,并且 y.equals(z) 返回 true,那么 x.equals(z) 应返回 true。
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
一致性:对于任何非空引用值 x 和 y,多次调用 x.equals(y) 始终返回 true 或始终返回 false,前提是对象上 equals 比较中所用的信息没有被修改。
For any non-null reference value x, x.equals(null) should return false.
对于任何非空引用值 x,x.equals(null) 都应返回 false。
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
Object 类的 equals 方法实现对象上差别可能性最大的相等关系;即,对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象时,此方法才返回 true(x == y 具有值 true)。
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
注意:当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。
@param   obj   the reference object with which to compare.
@return   true if this object is the same as the obj argument; false otherwise.
@see     hashCode()
@see     java.util.HashMap

public boolean equals(Object obj)
{
        return (this == obj);
在此我们看到关系运算符"=="所判断的是实例对象而不是实例对象的取值.这里我们不难推测关系运算符判断引用数据类型都是从实例对象出发的。
}

Creates and returns a copy of this object.  The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:
创建并返回此对象的一个副本。“副本”的准确含义可能依赖于对象的类。
要调用该方法必须继承Cloneable接口,否侧会抛出CloneNotSupportedException
这样做的目的是,对于任何对象 x,表达式:
x.clone() != x
will be true, and that the expression:
为 true,表达式:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
也为 true,但这些并非必须要满足的要求。一般情况下:
x.clone().equals(x)
will be true, this is not an absolute requirement.
为 true,但这并非必须要满足的要求。

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().
按照惯例,返回的对象应该通过调用 super.clone 获得。如果一个类及其所有的超类(Object 除外)都遵守此约定,则 x.clone().getClass() == x.getClass()。

By convention, the object returned by this method should be independent of this object (which is being cloned).  To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it.  Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.
按照惯例,此方法返回的对象应该独立于该对象(正被复制的对象)。要获得此独立性,在 super.clone 返回对象之前,有必要对该对象的一个或多个字段进行修改。这通常意味着要复制包含正在被复制对象的内部“深层结构”的所有可变对象,并使用对副本的引用替换对这些对象的引用。如果一个类只包含基本字段或对不变对象的引用,那么通常不需要修改 super.clone 返回的对象中的字段。

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
Object 类的 clone 方法执行特定的复制操作。首先,如果此对象的类不能实现接口 Cloneable,则会抛出 CloneNotSupportedException。注意,所有的数组都被视为实现接口 Cloneable。否则,此方法会创建此对象的类的一个新实例,并像通过分配那样,严格使用此对象相应字段的内容初始化该对象的所有字段;这些字段的内容没有被自我复制。所以,此方法执行的是该对象的“浅表复制”,而不“深层复制”操作。

The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.
Object 类本身不实现接口 Cloneable,所以在类为 Object 的对象上调用 clone 方法将会导致在运行时抛出异常。
在Object的子类中使用super.clone()不会抛出异常。
@return     a clone of this instance.
此实例的一个副本。
@exception  CloneNotSupportedException  if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.
如果对象的类不支持 Cloneable 接口,则重写 clone 方法的子类也会抛出此异常,以指示无法复制某个实例。
@see java.lang.Cloneable
protected native Object clone() throws CloneNotSupportedException;

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
返回该对象的字符串表示。通常,toString 方法会返回一个“以文本方式表示”此对象的字符串。结果应是一个简明但易于读懂的信息表达式。建议所有子类都重写此方法。
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character ‘@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
Object 类的 toString 方法返回一个字符串,该字符串由类名(对象是该类的一个实例)、at 标记符“@”和此对象哈希码的无符号十六进制表示组成。换句话说,该方法返回一个字符串,它的值等于:
getClass().getName() + '@' + Integer.toHexString(hashCode())

@return  a string representation of the object.
该对象的字符串表示形式。

public String toString()
{
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.
唤醒在此对象监视器上等待的单个线程。如果所有线程都在此对象上等待,则会选择唤醒其中一个线程。选择是任意性的,并在对实现做出决定时发生。线程通过调用其中一个 wait 方法,在对象的监视器上等待。
每个实例对象都有其对应的监视器侦听每一个拥有该对象引用的线程,而调用wait方法、notify方法及notifyAll方法的地方都是在synchronized对象同步块中。拥有对象同步快的线程,其监视器会根据系统的指示锁定或解开线程。当线程被锁定时,线程就停留在该行不往下执行了,只有锁定被解开才能往下执行。因此在线程执行到wait方法时锁必然是解开的,而当线程进入等待后该线程就没有必要解开锁了。以便于该实例对象让其他线程使用。Java虚拟机对这一部分的处理是释放等待线程的监视器。而当线程被其他线程唤醒时系统重新生成一个监视器侦听该线程。由于该线程唤醒时锁定不一定是解开的,因此如果要线程继续执行就必须解开锁定。
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
直到当前线程放弃此对象上的锁定,才能继续执行被唤醒的线程。被唤醒的线程将以常规方式与在该对象上主动同步的其他所有线程进行竞争;例如,唤醒的线程在作为锁定此对象的下一个线程方面没有可靠的特权或劣势。
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
此方法只应由作为此对象监视器的所有者的线程来调用。通过以下三种方法之一,线程可以成为此对象监视器的所有者:
By executing a synchronized instance method of that object.
通过执行此对象的同步实例方法。
By executing the body of a synchronized statement that synchronizes on the object.
通过执行在此对象上进行同步的 synchronized 语句的正文。
For objects of type Class, by executing a synchronized static method of that class.
对于 Class 类型的对象,可以通过执行该类的同步静态方法。
简单来说就是调用wait方法、notify方法及notifyAll方法的地方要用synchronized同步修饰词修饰。这里要注意一个问题,就是布尔型的赋值是生成一个新的对象来进行赋值的,也就是说赋值后的布尔型与之前的布尔型是两个不同的对象,如果我们同步修饰的对象时是布尔型的话,即监视器的对象是布尔型。在语句块中就不要对其进行赋值了。否则线程会因为对象的改变而失去对象监视器的所有权。
Only one thread at a time can own an object's monitor.
一次只能有一个线程拥有对象的监视器。

@exception  IllegalMonitorStateException  if the current thread is not the owner of this object's monitor.
如果当前线程不是此对象监视器的所有者。
@see        notifyAll()
@see        wait()
    public final native void notify();

Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.
唤醒在此对象监视器上等待的所有线程。线程通过调用其中一个 wait 方法,在对象的监视器上等待。
与notify方法相同,在唤醒的同时也要解开锁定该线程才能继续执行。

The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
直到当前线程放弃此对象上的锁定,才能继续执行被唤醒的线程。
这里所谓的放弃锁定其实就是解开锁定。
被唤醒的线程将以常规方式与在该对象上主动同步的其他所有线程进行竞争;例如,唤醒的线程在作为锁定此对象的下一个线程方面没有可靠的特权或劣势。

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
此方法只应由作为此对象监视器的所有者的线程来调用。有关线程能够成为监视器所有者的方法的描述,请参阅 notify 方法。

@exception  IllegalMonitorStateException  if the current thread is not the owner of this object's monitor.
如果当前线程不是此对象监视器的所有者。
@see        notify()
@see        wait()

public final native void notifyAll();

Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量前,导致当前线程等待。
此处的意思是wait方法要在其他线程调用对象的 notify方法或 notifyAll方法或超过指定的时间量前调用才有意义。
The current thread must own this object's monitor.
当前线程必须拥有此对象监视器。

This method causes the current thread (call it T ) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
此方法导致当前线程(称之为 T)将其自身放置在对象的等待集中,然后放弃此对象上的所有同步要求。
所谓放弃此对象上的所有同步要求即释放监视器。
出于线程调度目的,在发生以下四种情况之一前,线程 T 被禁用,且处于休眠状态:
Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
其他某个线程调用此对象的 notify 方法,并且线程 T 碰巧被任选为被唤醒的线程。
Some other thread invokes the notifyAll method for this object.
其他某个线程调用此对象的 notifyAll 方法。
Some other thread interrupt() interrupts thread T .
其他某个线程中断线程 T。
The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.
大约已经到达指定的实际时间。但是,如果 timeout 为零,则不考虑实际时间,在获得通知前该线程将一直等待。

The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. Thread T then returns from the invocation of the wait method. Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked.
然后,从对象的等待集中删除线程 T,并重新进行线程调度。然后,该线程以常规方式与其他线程竞争,以获得在该对象上同步的权利;一旦获得对该对象的控制权,该对象上的所有其同步声明都将被恢复到以前的状态,这就是调用 wait 方法时的情况。然后,线程 T 从 wait 方法的调用中返回。所以,从 wait 方法返回时,该对象和线程 T 的同步状态与调用 wait 方法时的情况完全相同。
A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:
在没有被通知、中断或超时的情况下,线程还可以唤醒一个所谓的虚假唤醒 (spurious wakeup)。虽然这种情况在实践中很少发生,但是应用程序必须通过以下方式防止其发生,即对应该导致该线程被提醒的条件进行测试,如果不满足该条件,则继续等待。换句话说,等待应总是发生在循环中,如下面的示例:
synchronized (obj)
{
while (<condition does not hold>)
{
obj.wait(timeout);
}
... // Perform action appropriate to condition
}
(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001)).
(有关这一主题的更多信息,请参阅 Doug Lea 撰写的 Concurrent Programming in Java (Second Edition) (Addison-Wesley, 2000) 中的第 3.2.3 节或 Joshua Bloch 撰写的 Effective Java Programming Language Guide (Addison-Wesley, 2001) 中的第 50 项。)

If the current thread is interrupt() interrupted} by any thread before or while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.
如果当前线程在等待之前或在等待时被任何线程中断,则会抛出 InterruptedException。在按上述形式恢复此对象的锁定状态时才会抛出此异常。
即异常将在唤醒时抛出。

Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.
注意,由于 wait 方法将当前线程放入了对象的等待集中,所以它只能解除此对象的锁定;可以同步当前线程的任何其他对象在线程等待时仍处于锁定状态。
即该线程所拥有的其他对象的监视器任然存在而且是锁定的,被释放的仅仅是包住wait方法的同步块的对象监视器而已。

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
此方法只应由作为此对象监视器的所有者的线程来调用。有关线程能够成为监视器所有者的方法的描述,请参阅 notify 方法。

@param      timeout   the maximum time to wait in milliseconds.
要等待的最长时间(以毫秒为单位)。
@exception  IllegalArgumentException      if the value of timeout is negative.
如果超时值为负。
@exception  IllegalMonitorStateException  if the current thread is not the owner of the object's monitor.
如果当前线程不是此对象监视器的所有者。
@exception  InterruptedException if any thread interrupted the current thread before or while the current thread was waiting for a notification.  The interrupted status of the current thread is cleared when this exception is thrown.
如果在当前线程等待通知之前或者正在等待通知时,任何线程中断了当前线程。在抛出此异常时,当前线程的中断状态 被清除。
@see        notify()
@see        notifyAll()

public final native void wait(long timeout) throws InterruptedException;
此处对照“两参数wait方法”以及api给出的相关异常列表就会发现IllegalArgumentException和IllegalMonitorStateException并没有通过java语言来实现,这是因为native修饰词修饰之后,就没有方法体了。也就是说检验timeout合理性的部分在底层实现。而为了保险起见“两参数wait方法”内部,对timeout又进行了一次合理性检查,其实该检查与底层是重复了的。

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者其他某个线程中断当前线程,或者已超过某个实际时间量前,导致当前线程等待。

This method is similar to the wait method of one argument, but it allows finer control over the amount of time to wait for a notification before giving up. The amount of real time, measured in nanoseconds, is given by:
此方法类似于一个参数的 wait 方法,但它允许更好地控制在放弃之前等待通知的时间量。用毫微秒度量的实际时间量可以通过以下公式计算出来:
1000000*timeout+nanos

In all other respects, this method does the same thing as the method wait(long) of one argument. In particular, wait(0, 0) means the same thing as wait(0).
在其他所有方面,此方法执行的操作与带有一个参数的 wait(long) 方法相同。需要特别指出的是,wait(0, 0) 与 wait(0) 相同。

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:
当前线程必须拥有此对象监视器。该线程发布对此监视器的所有权,并等待下面两个条件之一发生:
Another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method.
其他线程通过调用 notify 方法,或 notifyAll 方法通知在此对象的监视器上等待的线程醒来。
The timeout period, specified by timeout milliseconds plus nanos nanoseconds arguments, has elapsed.
timeout 毫秒值与 nanos 毫微秒参数值之和指定的超时时间已用完。
The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
然后,该线程等到重新获得对监视器的所有权后才能继续执行。
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
对于某一个参数的版本,实现中断和虚假唤醒是有可能的,并且此方法应始终在循环中使用:
synchronized (obj)
{
while (<condition does not hold>)
{
obj.wait(timeout, nanos);
}
... // Perform action appropriate to condition
}
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
此方法只应由作为此对象监视器的所有者的线程来调用。有关线程能够成为监视器所有者的方法的描述,请参阅 notify 方法。

@param      timeout   the maximum time to wait in milliseconds.
要等待的最长时间(以毫秒为单位)。
@param     nanos      additional time, in nanoseconds range 0-999999.
额外时间(以毫微秒为单位,范围是 0-999999)。
@exception  IllegalArgumentException      if the value of timeout is negative or the value of nanos is not in the range 0-999999.
如果超时值是负数,或者毫微秒值不在 0-999999 范围内。
@exception  IllegalMonitorStateException  if the current thread is not the owner of this object's monitor.
如果当前线程不是此对象监视器的所有者。
@exception  InterruptedException if any thread interrupted the current thread before or while the current thread was waiting for a notification.  The interrupted status of the current thread is cleared when this exception is thrown.
如果在当前线程等待通知之前或者正在等待通知时,任何线程中断了当前线程。在抛出此异常时,当前线程的中断状态 被清除。

public final void wait(long timeout, int nanos) throws InterruptedException
{
检查timeout合理性
if (timeout < 0)
{
throw new IllegalArgumentException("timeout value is negative");
}
检查nanos合理性
if (nanos < 0 || nanos > 999999)
{
throw new IllegalArgumentException("nanosecond timeout value out of range");
}
        进行四舍五入运算。
此处可以发现,其实“两参数wait方法”也不精确。nanos只是做了粗略的处理最后还是通过“一参数wait方法”来实现。也就是说其实nanos等于1和nanos等于499999效果将是一样的。
if (nanos >= 500000 || (nanos != 0 && timeout == 0))
{
timeout++;
}
wait(timeout);
}

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).
在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。换句话说,此方法的行为就好像它仅执行 wait(0) 调用一样。

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
当前线程必须拥有此对象监视器。该线程发布对此监视器的所有权并等待,直到其他线程通过调用 notify 方法,或 notifyAll 方法通知在此对象的监视器上等待的线程醒来。然后该线程将等到重新获得对监视器的所有权后才能继续执行。

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
对于某一个参数的版本,实现中断和虚假唤醒是可能的,而且此方法应始终在循环中使用:
synchronized (obj)
{
while (<condition does not hold>)
{
obj.wait();
}
... // Perform action appropriate to condition
}

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
此方法只应由作为此对象监视器的所有者的线程来调用。有关线程能够成为监视器所有者的方法的描述,请参阅 notify 方法。

@exception  IllegalMonitorStateException  if the current thread is not the owner of the object's monitor.
如果当前线程不是此对象监视器的所有者。
@exception  InterruptedException if any thread interrupted the current thread before or while the current thread was waiting for a notification.  The interrupted status of the current thread is cleared when this exception is thrown.
如果在当前线程等待通知之前或者正在等待通知时,任何线程中断了当前线程。在抛出此异常时,当前线程的中断状态 被清除。
@see        notify()
@see        notifyAll()

public final void wait() throws InterruptedException
{
        wait(0);
}

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.
当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。子类重写 finalize 方法,以配置系统资源或执行其他清除。
子类如果想在释放该对象的同时释放掉其他对象的话可以重写该方法,但要注意如释放的对象仍然有引用的话容易出现错误,抛出异常。

The general contract of finalize is that it is invoked if and when the Java TM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.
finalize 的常规协定是:当 JavaTM 虚拟机已确定尚未终止的任何线程无法再通过任何方法访问此对象时,将调用此方法,除非由于准备终止的其他某个对象或类的终结操作执行了某个操作。finalize 方法可以采取任何操作,其中包括再次使此对象对其他线程可用;不过,finalize 的主要目的是在不可撤消地丢弃对象之前执行清除操作。例如,表示输入/输出连接的对象的 finalize 方法可执行显式 I/O 事务,以便在永久丢弃对象之前中断连接。

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.
Object 类的 finalize 方法执行非特殊性操作;它仅执行一些常规返回。Object 的子类可以重写此定义。

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.
Java 编程语言不保证哪个线程将调用某个给定对象的 finalize 方法。但可以保证在调用 finalize 时,调用 finalize 的线程将不会持有任何用户可见的同步锁定。如果 finalize 方法抛出未捕获的异常,那么该异常将被忽略,并且该对象的终结操作将终止。

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.
在启用某个对象的 finalize 方法后,将不会执行进一步操作,直到 Java 虚拟机再次确定尚未终止的任何线程无法再通过任何方法访问此对象,其中包括由准备终止的其他对象或类执行的可能操作,在执行该操作时,对象可能被丢弃。

The finalize method is never invoked more than once by a Java virtual machine for any given object.
对于任何给定对象,Java 虚拟机最多只调用一次 finalize 方法。

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.
finalize 方法抛出的任何异常都会导致此对象的终结操作停止,但可以通过其他方法忽略它。

@throws Throwable the Exception raised by this method
此方法抛出的 Exception

    protected void finalize() throws Throwable { }
}
0
2
分享到:
评论

相关推荐

    java.lang.IllegalArgumentException Cannot format given Object as a Date

    在Java编程中,`java.lang.IllegalArgumentException` 是一个标准的运行时异常,它通常表示一个方法接收到的参数值不在预期范围内或者不合法。当尝试将一个无法转换为日期的对象格式化时,就会抛出"Cannot format ...

    java.lang.runtimeexception unexpected subelement

    在Java编程中,`java.lang.RuntimeException` 是一个非检查异常(unchecked exception),它表示程序运行时出现的不正常情况。通常,这类异常是由于程序员的错误或代码逻辑的疏忽造成的,例如类型转换错误、空指针...

    WebLogic错误 java.lang.AssertionError: Registered more than one

    Caused by: java.lang.RuntimeException: javax.management.remote.JMXServerErrorException: java.lang.AssertionError: Registered more than one instance with the same objectName : com.bea:Name=wl04,Type=...

    java.lang包介绍

    `java.lang`包中最基本的类是`Object`,它是所有类层次结构的根,而`Class`类的实例则代表运行时的类。 `java.lang`包包括以下关键类: 1. **Boolean**: 表示布尔值`true`和`false`。 2. **Long, Short, Byte, ...

    Java.lang.reflect 包下常用的类及方法简介

    每个类在内存中都有一个唯一的`Class`实例,可以通过`Class.forName()`方法获取,或者通过`Object.getClass()`方法从对象中获取。 - `getConstructors()`:返回类的所有公共构造函数。 - `getDeclaredConstructors...

    android java.lang.IllegalArgumentException contains a path separator

    在Android开发中,Java编程时常会遇到`java.lang.IllegalArgumentException`异常,这通常表示传入的参数不符合方法的要求。在本例中,错误信息“contains a path separator”提示我们问题可能与路径分隔符有关,这...

    java.lang.NoSuchMethodException: .getPortClassMap()

    在Java编程中,`java.lang.NoSuchMethodException` 是一个常见的运行时异常,它表示尝试调用一个不存在的方法。这个异常通常发生在动态方法调用或反射操作中。在给出的标题 "java.lang.NoSuchMethodException: ....

    Java总结篇系列:java.lang.Object

     Java作为一个庞大的知识体系,涉及到的知识点繁多,本文将从Java中基本的类java.lang.Object开始谈起。  Object类是Java中其他所有类的祖先,没有Object类Java面向对象无从谈起。作为其他所有类的基类,Object...

    java.lang包

    `Object`类是所有Java类的最终超类,包含所有对象共有的属性和方法,如`equals()`比较两个对象是否相等,`hashCode()`返回对象的哈希码,`toString()`返回对象的字符串表示。 通过理解并熟练使用`java.lang`包中的...

    在Java中异常分析

    - **异常路径**:`java.lang.Object -&gt; java.lang.Throwable -&gt; java.lang.Exception -&gt; java.lang.RuntimeException -&gt; java.lang.IndexOutOfBoundsException -&gt; java.lang.ArrayIndexOutOfBoundsException` ...

    java.lang包.ppt

    了解 java.lang 包 掌握包装类 掌握String 和 StringBuffer 类 运用以下类的方法: Math Class Object

    java.lang.object源码-finalize-:个人博客(http://bit.ly/javafinalize)上java.lang

    java.lang.object原始码最终不推荐使用java.lang.Object.finalize() 我的博客文章中的示例的Gradle源代码存储库。 在Linux Mint(SUSE Linux)上构建和测试。 由于gradle当前不支持Java 11,因此可以使用标准Java...

    java.lang.ClassNotFoundException: org.jdom.input.SAXBuilder 处理

    总之,解决“java.lang.ClassNotFoundException: org.jdom.input.SAXBuilder”问题的关键在于确保JDOM库被正确地包含在类路径中,同时检查代码中涉及的类名拼写是否准确。了解并熟练使用SAXBuilder可以帮助开发者...

    javax.activation.UnsupportedDataTypeException: no object DCH for MIME type

    在Oracle 11.2.0.4中调用 javax.mail 发送邮件时...EXEC dbms_java.grant_permission( '&lt;your_schema_please_change&gt;','SYS:java.lang.RuntimePermission', 'setContextClassLoader', '' ); 第二步:解决方案看文档中。

    java.lang.ClassCastException

    在Java编程中,`java.lang.ClassCastException`是一个运行时异常,通常发生在强制类型转换时,如果尝试将一个对象转换为其非兼容类型时就会抛出。这个异常表明在编译时可能成功的类型转换,在运行时被证明是错误的。...

    maven3.6.3资源包

    1) Error injecting constructor, java.lang.NoSuchMethodError: org.apache.maven.model.validation.DefaultModelValidator: method 'void ()' not found at org.jetbrains.idea.maven.server.embedder....

    Java类库复习——java.lang.String

    在Java编程语言中,`java.lang.String`是最重要的类之一,它是所有字符串操作的基础。这个类位于核心类库中,因此无需显式导入即可使用。本文将深入探讨`String`类的一些关键知识点,包括它的特性、构造方法、常用...

    java.lang研究

    `Object`类是所有Java类的根,它定义了所有对象共有的行为,如`equals()`、`hashCode()`和`toString()`方法。`System`类提供了系统级的服务,如标准输入、输出流,以及全局变量`out`、`err`和`in`。 `Thread`类是多...

    jdbc 帮助类 java 自带连接池 v1.01

    boolean delete(java.lang.String sql, java.util.Map&lt;java.lang.Integer,java.lang.Object&gt; elements) 根据传入的参数删除单条记录的方法 boolean delete(java.lang.String sql, java.lang.Object[] elements)...

Global site tag (gtag.js) - Google Analytics