【翻译】finalize方法到底要干嘛
2015-03-19 17:43
204 查看
以下是JDK中的描述:
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方法。子类重写finalize方法,一般是为了处理系统资源或者做一些其他的清理工作。
The general contract of
of some other object or class which is ready to be finalized. The
before the object is permanently discarded.
一般来说,finalze方法都是在Java虚拟机发现去除那些已经被执行了finalize的对象之外,没有任何活动的线程能够引用到该对象的时候调用。在finalze方法里,可以做任何事情,甚至让该对象重新可被其他线程引用;但是一般来说,finalize方法的通常目的是在该对象真正被回收之前做一些清理的工作。比如,一个代表输入/输出连接的对象,它的finalize方法可能会在自己被回收之前中断对应的I/O连接。
The
Object类的finalize方法没做任何事,只是简单的返回。而Object的子类可以重写这个方法。
The Java programming language does not guarantee which thread will invoke the
the exception is ignored and finalization of that object terminates.
Java语言不保证哪个线程会调用finalize方法。但是却保证那个调用finalize方法的线程,会在finalize被调用的时候持有任何用户可见的同步锁。如果finalize方法中抛出了一个没有被catch的异常,这个异常会被忽略,finalize方法会中止。
After the
possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.
一个对象的finalize方法被调用之后,这个对象就不可以被任何活动的线程再次引用了,这个对象也不能再进行任何的操作,包括被那些已经执行finalize方法的对象或者类,直到被Java虚拟机回收。
The
一个特定的对象的finalize方法绝对不会被Java虚拟机调用超过一次。
Any exception thrown by the
finalize方法抛出的任何异常,会导致finalize方法的中止,但是这也会被忽略。(意思应该说是,虽然抛出了异常,finalize中止,没法继续执行,但是再此之后,对象还是会不可以被操作,直到它被Java虚拟机回收掉。)
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
finalizemethod to dispose of system resources or to perform other cleanup.
当垃圾回收器发现一个对象没有任何引用的时候,就会调用这个对象的类对应的finalize方法。子类重写finalize方法,一般是为了处理系统资源或者做一些其他的清理工作。
The general contract of
finalizeis that it is invoked if and when the Java™ 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
finalizemethod 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.
一般来说,finalze方法都是在Java虚拟机发现去除那些已经被执行了finalize的对象之外,没有任何活动的线程能够引用到该对象的时候调用。在finalze方法里,可以做任何事情,甚至让该对象重新可被其他线程引用;但是一般来说,finalize方法的通常目的是在该对象真正被回收之前做一些清理的工作。比如,一个代表输入/输出连接的对象,它的finalize方法可能会在自己被回收之前中断对应的I/O连接。
The
finalizemethod of class
Objectperforms no special action; it simply returns normally. Subclasses of
Objectmay override this definition.
Object类的finalize方法没做任何事,只是简单的返回。而Object的子类可以重写这个方法。
The Java programming language does not guarantee which thread will invoke the
finalizemethod 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方法中抛出了一个没有被catch的异常,这个异常会被忽略,finalize方法会中止。
After the
finalizemethod 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方法被调用之后,这个对象就不可以被任何活动的线程再次引用了,这个对象也不能再进行任何的操作,包括被那些已经执行finalize方法的对象或者类,直到被Java虚拟机回收。
The
finalizemethod is never invoked more than once by a Java virtual machine for any given object.
一个特定的对象的finalize方法绝对不会被Java虚拟机调用超过一次。
Any exception thrown by the
finalizemethod causes the finalization of this object to be halted, but is otherwise ignored.
finalize方法抛出的任何异常,会导致finalize方法的中止,但是这也会被忽略。(意思应该说是,虽然抛出了异常,finalize中止,没法继续执行,但是再此之后,对象还是会不可以被操作,直到它被Java虚拟机回收掉。)
相关文章推荐
- 搞了一天终于实现了 DataGrid 中的多国语言的问题,采用了中文向其他语言自动翻译的方法,其实不难,主要是想法(本文不涉及这个计算机技术问题,仅以此作为标题)
- Finalize方法应该干什么?
- 论习语的翻译方法
- 关于JAVA垃圾收集器与类的finalize()方法的一些总结
- C#中Finalize方法的问题
- [翻译]抓屏的各种方法
- 聊天室的自动定位显示到底部的方法
- RFC1961中文翻译 GSS-API对SOCKS V5 的认证方法(GSS-API Authentication Method for SOCKS Version 5)
- 对象析构和finalize方法
- 用搜索引擎寻找中文的英文翻译方法4种
- [翻译]抓屏的各种方法
- [翻译] Effective C++, 3rd Edition, Item 35: 考虑可选的 virtual functions(虚拟函数)的替代方法(上)
- 总的来说,ASP.NET到底是什么哪?(翻译)
- 在baidu上搜到自己以前翻译的文章:C++代码检查方法
- 重写 Finalize 方法
- [翻译]对使用绑定列的DataGrid进行排序的另一种方法
- [翻译]抓屏的各种方法
- 聊天室的自动定位显示到底部的方法
- [翻译]用DataSource控件以外的方法为GridView提供数据
- Gang of Four,到底翻译成是“四人帮“还是”四人组“