[Java Doc]reference翻译
oracle doc原文:https://docs.oracle.com/javase/9/docs/api/java/lang/ref/package-summary.html#reachability
Package java.lang.ref
since: 1.2
Provides reference-object classes, which support a limited degree of interaction with the garbage collector. A program may use a reference object to maintain a reference to some other object in such a way that the latter object may still be reclaimed by the collector. A program may also arrange to be notified some time after the collector has determined that the reachability of a given object has changed.
提供引用对象类,它们支持同垃圾收集器有限程度的交互。程序可以使用一个引用对象来维护对某个其他对象的引用,利用这样的方式,后一个对象仍然可以被收集器回收。程序也可以安排在收集器确定给定对象的可达性已改变之后,于某个时候得到通知。
notify v 通知,唤醒
Package Specification
包描述
A reference object encapsulates a reference to some other object so that the reference itself may be examined and manipulated like any other object. Three types of reference objects are provided, each weaker than the last: soft, weak, and phantom. Each type corresponds to a different level of reachability, as defined below. Soft references are for implementing memory-sensitive caches, weak references are for implementing canonicalizing mappings that do not prevent their keys (or values) from being reclaimed, and phantom references are for scheduling post-mortem cleanup actions. Post-mortem cleanup actions can be registered and managed by a Cleaner
.
引用对象封装某个其他对象的引用,这使得引用对象本身可以像任何其他对象一样被检查和操作。提供三种引用对象,由从强到弱的顺序是:软引用(soft),弱引用(*weak)*,虚引用( phantom)。每种类型都对应一个不同的可达度(GC中的可达度),定义如下。软引用用于实现对内存敏感的缓存,弱引用用于实现不阻止键(或值)被回收的规范化映射,虚引用用于调度事后清理的操作。事后清理行为可以由Cleaner对象注册和管理。
encapsulate v 简述,概括,压缩
canonicalizing v 规范化
examine 检查
Each reference-object type is implemented by a subclass of the abstract base Reference
class. An instance of one of these subclasses encapsulates a single reference to a particular object, called the referent. Every reference object provides methods for getting and clearing the reference. Aside from the clearing operation reference objects are otherwise immutable, so no set
operation is provided. A program may further subclass these subclasses, adding whatever fields and methods are required for its purposes, or it may use these subclasses without change.
每个引用对象都由抽象基类 Reference的子类实现。一个这些子类的实例封装了一个对于特定对象的单个引用,这个特定对象被叫做所指对象( referent ,指称)。每种引用对象都提供了获取和清理引用的方法。除了清理操作以外,引用对象是不可变的,所以不提供设置操作。程序可以扩展这些子类出新的子类,出于程序的目的来添加任何字段域(field)和方法(method),或者不加任何改变来使用这些子类。
otherwise adv 否则,不然,反之
immutable adj 不可变
Notification
通知,公示
A program may request to be notified of changes in an object’s reachability by registering an appropriate reference object with a reference queue at the time the reference object is created. Some time after the garbage collector determines that the reachability of the referent has changed to the value corresponding to the type of the reference, it will clear the reference and add it to the associated queue. At this point, the reference is considered to be enqueued. The program may remove references from a queue either by polling or by blocking until a reference becomes available. Reference queues are implemented by the ReferenceQueue
class.
程序可以通过在创建引用对象时,用引用队列注册一个合适的引用对象的方式,来请求通知对象的可达性的变化。在垃圾收集器决定所指对象 (referent) 的可达性变更到相应类型的值后的一段时间,垃圾收集器会清除引用并添加它到关联的队列中。此时,该引用被认为已入队了。程序通过轮询 (poll) 和阻塞 (blocking) 的方式,从队列中删除引用 (reference) ,直到引用 (reference) 变得的再次可达。引用队列由 ReferenceQueue 类实现。
appropriate adj 合适的,恰当的
corresponding to 相关的,对应的
associated 关联的
either .. or … 两者任一
The relationship between a registered reference object and its queue is one-sided. That is, a queue does not keep track of the references that are registered with it. If a registered reference becomes unreachable itself, then it will never be enqueued. It is the responsibility of the program using reference objects to ensure that the objects remain reachable for as long as the program is interested in their referents.
注册的引用对象和它的队列的关系是片面的。这是因为,队列并不跟踪其注册的引用们(references)。如果一个注册的引用自身变得不可达,之后它将永远不会入队。只要程序还关联引用所指对象(referent),程序有责任,使用引用来确保对象的可访问性。
one-sided 片面的
While some programs will choose to dedicate a thread to removing reference objects from one or more queues and processing them, this is by no means necessary. A tactic that often works well is to examine a reference queue in the course of performing some other fairly-frequent action. For example, a hashtable that uses weak references to implement weak keys could poll its reference queue each time the table is accessed. This is how the WeakHashMap
class works. Because the ReferenceQueue.poll
method simply checks an internal data structure, this check will add little overhead to the hashtable access methods.
虽然一些程序会选择牺牲一个线程从一个或更多的队列删除引用对象和处理它们,但这绝不是必要的。一个通常工作很好的策略,是进行某种相当频繁的操作的时候,检查引用队列。例如,hashtable使用弱引用来实现弱键,当每次访问table的时候,可以轮询它的引用队列。这也是WeakHashMap类的工作原理。因为ReferenceQueue.poll 方法只是检查内部的数据结构,这项检查不会为hashtable访问方法添加额外的开销。
dedicate 奉献. dedicate/devote/consecrate sth to doing
by no means 绝不 this is by no means necessary
tactic n 策略
in the course of
fairly adv quite, considerably 相当地
Automatically-cleared references
自动清理的引用们
Soft and weak references are automatically cleared by the collector before being added to the queues with which they are registered, if any. Therefore soft and weak references need not be registered with a queue in order to be useful, while phantom references do. An object that is reachable via phantom references will remain so until all such references are cleared or themselves become unreachable.
软引用和弱引用在被添加到它们注册的队列中之前,会被垃圾收集器自动清理(如果有的话)。因此为了有效,软引用和弱引用不需要被注册到一个对列中,而虚引用需要添加到队列中。一个可以通过虚引用可达的对象将保持不变,直到所有引用被清除,或者对象自身变得不可达。
Reachability
可达性
Going from strongest to weakest, the different levels of reachability reflect the life cycle of an object. They are operationally defined as follows:
从最强到最弱,不同可达性的等级反映了一个对象的生命周期。他们操作上被定义为下列:
operationally adj 操作上
- An object is strongly reachable if it can be reached by some thread without traversing any reference objects. A newly-created object is strongly reachable by the thread that created it.
- An object is softly reachable if it is not strongly reachable but can be reached by traversing a soft reference.
- An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing a weak reference. When the weak references to a weakly-reachable object are cleared, the object becomes eligible for finalization.
- An object is phantom reachable if it is neither strongly, softly, nor weakly reachable, it has been finalized, and some phantom reference refers to it.
- Finally, an object is unreachable, and therefore eligible for reclamation, when it is not reachable in any of the above ways.
(Strong)如果对象可以被某个线程不用通过任何引用对象访问,这个对象是强可达的。一个新建的对象对于创建它的线程来讲是强可达的。
(Soft)如果对象不是强可达,但是可以通过软引用(SoftReference)访问到,它是软可达的。
(Weak)如果对象既不是强可达,也不是软可达,但可以通过弱引用(WeakReference)访问到,它是弱可达的。当指向弱引用的弱引用被清除,对象就具备了结束(finalization)的条件。
(phantom) 如果对象既不是强可达,软可达,也不是弱可达,对象是虚可达(PhantomReference)。它已经被结束了(finalize),并且某个虚引用指向它。
最终,当对象无法通过上述任何一种方式到达时,对象是不可达状态(unreachable),因此具备了进行回收的资格。
reclamation n 回收
Interface Summary
接口摘要
Interface | Description |
---|---|
Cleaner.Cleanable | Cleanable represents an object and a cleaning action registered in a Cleaner . |
Cleanable 表示在 Cleaner 上注册的对象和清理工作。注:Cleanable接口只有一个clean()方法。 |