-
Notifications
You must be signed in to change notification settings - Fork 668
/
Cleaner.java
165 lines (152 loc) · 6.22 KB
/
Cleaner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.ref;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* General-purpose phantom-reference-based cleaners.
*
* <p> Cleaners are a lightweight and more robust alternative to finalization.
* They are lightweight because they are not created by the VM and thus do not
* require a JNI upcall to be created, and because their cleanup code is
* invoked directly by the reference-handler thread rather than by the
* finalizer thread. They are more robust because they use phantom references,
* the weakest type of reference object, thereby avoiding the nasty ordering
* problems inherent to finalization.
*
* <p> A cleaner tracks a referent object and encapsulates a thunk of arbitrary
* cleanup code. Some time after the GC detects that a cleaner's referent has
* become phantom-reachable, the reference-handler thread will run the cleaner.
* Cleaners may also be invoked directly; they are thread safe and ensure that
* they run their thunks at most once.
*
* <p> Cleaners are not a replacement for finalization. They should be used
* only when the cleanup code is extremely simple and straightforward.
* Nontrivial cleaners are inadvisable since they risk blocking the
* reference-handler thread and delaying further cleanup and finalization.
*
* @author Mark Reinhold
*/
/*
* 虚引用清理器(非公开)
*
* 虚引用的子类,用作清理器。当虚引用被回收时,在Reference中回调此类的清理功能。
* 一个程序可以有多个Cleaner。多个Cleaner组成了一个双向链表。
*/
public class Cleaner extends PhantomReference<Object> {
/*
* Dummy reference queue, needed because the PhantomReference constructor insists that we pass a queue.
* Nothing will ever be placed on this queue since the reference handler invokes cleaners explicitly.
*
* 声明此字段的原因是虚引用构造方法必须使用一个ReferenceQueue参数
* 实际上,被GC回收的对象,其相应的"虚引用清理器"不会被加入到此队列中。
* 可参见Reference类的processPendingReferences()方法。
*/
private static final ReferenceQueue<Object> dummyQueue = new ReferenceQueue<>();
// 清理动作
private final Runnable thunk;
/* Doubly-linked list of live cleaners, which prevents the cleaners themselves from being GC'd before their referents */
// 双向链表头部
private static Cleaner first = null;
// 双向链表前驱和后继
private Cleaner next = null, prev = null;
// 注册追踪的引用obj和清理动作thunk
private Cleaner(Object referent, Runnable thunk) {
super(referent, dummyQueue);
this.thunk = thunk;
}
/**
* Creates a new cleaner.
*
* @param ob the referent object to be cleaned
* @param thunk The cleanup code to be run when the cleaner is invoked. The
* cleanup code is run directly from the reference-handler thread,
* so it should be as simple and straightforward as possible.
*
* @return The new cleaner
*/
// 创建一个Cleaner,并将其添加到双向链表中
public static Cleaner create(Object ob, Runnable thunk) {
if(thunk == null)
return null;
return add(new Cleaner(ob, thunk));
}
// 使用头插法将Cleaner添加到双向链表中
private static synchronized Cleaner add(Cleaner cl) {
if(first != null) {
cl.next = first;
first.prev = cl;
}
first = cl;
return cl;
}
// 移除一个Cleaner
private static synchronized boolean remove(Cleaner cl) {
// If already removed, do nothing
if(cl.next == cl)
return false;
// Update list
if(first == cl) {
if(cl.next != null)
first = cl.next;
else
first = cl.prev;
}
if(cl.next != null)
cl.next.prev = cl.prev;
if(cl.prev != null)
cl.prev.next = cl.next;
// Indicate removal by pointing the cleaner to itself
cl.next = cl;
cl.prev = cl;
return true;
}
/**
* Runs this cleaner, if it has not been run before.
*/
// 对追踪对象进行清理,在Reference类中完成
public void clean() {
// 已移除的不再执行其功能,保证每个Cleaner只发挥一次作用
if(!remove(this)) {
return;
}
try {
// 执行清理
thunk.run();
} catch(final Throwable x) {
AccessController.doPrivileged(new PrivilegedAction<>() {
public Void run() {
if(System.err != null) {
new Error("Cleaner terminated abnormally", x).printStackTrace();
}
System.exit(1);
return null;
}
});
}
}
}