-
Notifications
You must be signed in to change notification settings - Fork 668
/
CleanerImpl.java
415 lines (373 loc) · 14.5 KB
/
CleanerImpl.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* Copyright (c) 2015, 2016, 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 jdk.internal.misc.InnocuousThread;
import java.lang.ref.Cleaner;
import java.lang.ref.Cleaner.Cleanable;
import java.lang.ref.ReferenceQueue;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
/**
* CleanerImpl manages a set of object references and corresponding cleaning actions.
* CleanerImpl provides the functionality of {@link java.lang.ref.Cleaner}.
*/
// 为清理器(Cleaner)提供清理服务,与Cleaner是一对一关系
public final class CleanerImpl implements Runnable {
/**
* An object to access the CleanerImpl from a Cleaner; set by Cleaner init.
*/
// cleaner -> cleaner.impl,给定Cleaner对象,返回其内部的CleanerImpl
private static Function<Cleaner, CleanerImpl> cleanerImplAccess = null;
/**
* Heads of a CleanableList for each reference type.
*/
final SoftCleanable<?> softCleanableList; // 软引用清理器列表
final WeakCleanable<?> weakCleanableList; // 弱引用清理器列表
final PhantomCleanable<?> phantomCleanableList; // 虚引用清理器列表,是清理器Cleaner的默认实现
/*
* The ReferenceQueue of pending cleaning actions
*
* 存放报废的Reference,在这里存放的是报废的引用清理器,因为引用清理器本身也是Reference。
*/
final ReferenceQueue<Object> queue;
/**
* Constructor for CleanerImpl.
*/
// 完成引用队列和引用链表的初始化
public CleanerImpl() {
queue = new ReferenceQueue<>();
softCleanableList = new SoftCleanableRef();
weakCleanableList = new WeakCleanableRef();
phantomCleanableList = new PhantomCleanableRef();
}
/**
* Called by Cleaner static initialization to provide the function to map from Cleaner to CleanerImpl.
*
* @param access a function to map from Cleaner to CleanerImpl
*/
// 提供函数式接口,跟普通的set方法作用一样
public static void setCleanerImplAccess(Function<Cleaner, CleanerImpl> access) {
if(cleanerImplAccess == null) {
cleanerImplAccess = access;
} else {
throw new InternalError("cleanerImplAccess");
}
}
/**
* Called to get the CleanerImpl for a Cleaner.
*
* @param cleaner the cleaner
*
* @return the corresponding CleanerImpl
*/
// 给定Cleaner对象,返回其内部的CleanerImpl
static CleanerImpl getCleanerImpl(Cleaner cleaner) {
return cleanerImplAccess.apply(cleaner);
}
/**
* Starts the Cleaner implementation.
* Ensure this is the CleanerImpl for the Cleaner.
* When started waits for Cleanables to be queued.
*
* @param cleaner the cleaner
* @param threadFactory the thread factory
*/
// 将清理服务放入守护线程,并启动它,主要作用是轮询ReferenceQueue,取出其中报废的Reference,执行其清理动作action
public void start(Cleaner cleaner, ThreadFactory threadFactory) {
// Cleaner和CleanerImpl是一对一关系
if(getCleanerImpl(cleaner) != this) {
throw new AssertionError("wrong cleaner");
}
/*
* schedule a nop cleaning action for the cleaner,
* so the associated thread will continue to run at least until the cleaner is reclaimable.
*/
// 将Cleaner对象自身也加入追踪
new CleanerCleanable(cleaner);
// 使用清理器内部默认的"无害"线程工厂
if(threadFactory == null) {
threadFactory = CleanerImpl.InnocuousThreadFactory.factory();
}
/*
* now that there's at least one cleaning action, for the cleaner, we can start the associated thread, which runs until all cleaning actions have been run.
*/
// 将专属清理服务绑定到专属的守护线程
Thread thread = threadFactory.newThread(this);
thread.setDaemon(true);
// 调用下面的run()方法,提供清理服务
thread.start();
}
/**
* Process queued Cleanables as long as the cleanable lists are not empty.
* A Cleanable is in one of the lists for each Object and for the Cleaner itself.
* Terminates when the Cleaner is no longer reachable and has been cleaned and there are no more Cleanable instances for which the object is reachable.
* <p>
* If the thread is a ManagedLocalsThread, the threadlocals are erased before each cleanup
*/
// 提供清理服务,最终调用清理器的clean方法
@Override
public void run() {
Thread t = Thread.currentThread();
// "无害"线程,如果外界不指定自定义的线程,那么清理器内部默认使用此类型线程做守护线程
InnocuousThread mlThread = (t instanceof InnocuousThread) ? (InnocuousThread) t : null;
// 轮询清理器列表
while(!phantomCleanableList.isListEmpty()
|| !weakCleanableList.isListEmpty()
|| !softCleanableList.isListEmpty()) {
if(mlThread != null) {
// 即清除该线程中所有ThreadLocal信息
mlThread.eraseThreadLocals();
}
// 清理服务的核心:轮询"报废引用"队列,取出被回收引用对应的虚引用(清理器),执行其清理动作
try {
// Wait for a Ref, with a timeout to avoid getting hung due to a race with clear/clean
Cleanable ref = (Cleanable) queue.remove(60 * 1000L); // 设置轮询时间阙值,超时则陷入阻塞
if(ref != null) {
ref.clean(); // 执行清理动作
}
} catch(Throwable e) {
// ignore exceptions from the cleanup action (including interruption of cleanup thread)
}
}
}
/**
* Perform cleaning on an unreachable SoftReference.
*/
/*
* Reference
* │
* SoftReference Cleanable
* └──────┬────────┘
* SoftCleanable
* │
* SoftCleanableRef
*
* 软引用清理器,此类的属性既是虚引用,又是清理器
*/
public static final class SoftCleanableRef extends SoftCleanable<Object> {
private final Runnable action;
/**
* Constructor for a soft cleanable reference.
*
* @param obj the object to monitor
* @param cleaner the cleaner
* @param action the action Runnable
*/
// 向cleaner注册跟踪的对象obj和清理动作action
SoftCleanableRef(Object obj, Cleaner cleaner, Runnable action) {
super(obj, cleaner);
this.action = action;
}
/**
* Constructor used only for root of soft cleanable list.
*/
SoftCleanableRef() {
super();
this.action = null;
}
/**
* Prevent access to referent even when it is still alive.
*
* @throws UnsupportedOperationException always
*/
@Override
public Object get() {
throw new UnsupportedOperationException("get");
}
/**
* Direct clearing of the referent is not supported.
*
* @throws UnsupportedOperationException always
*/
@Override
public void clear() {
throw new UnsupportedOperationException("clear");
}
// CleanerImpl.run()==>清理器clean()-->清理器performCleanup()-->action.run()
@Override
protected void performCleanup() {
action.run();
}
}
/**
* Perform cleaning on an unreachable WeakReference.
*/
/*
* Reference
* │
* WeakReference Cleanable
* └──────┬────────┘
* WeakCleanable
* │
* WeakCleanableRef
*
* 虚引用清理器,此类的属性既是虚引用,又是清理器
*/
public static final class WeakCleanableRef extends WeakCleanable<Object> {
private final Runnable action;
/**
* Constructor for a weak cleanable reference.
*
* @param obj the object to monitor
* @param cleaner the cleaner
* @param action the action Runnable
*/
// 向cleaner注册跟踪的对象obj和清理动作action
WeakCleanableRef(Object obj, Cleaner cleaner, Runnable action) {
super(obj, cleaner);
this.action = action;
}
/**
* Constructor used only for root of weak cleanable list.
*/
WeakCleanableRef() {
super();
this.action = null;
}
/**
* Prevent access to referent even when it is still alive.
*
* @throws UnsupportedOperationException always
*/
@Override
public Object get() {
throw new UnsupportedOperationException("get");
}
/**
* Direct clearing of the referent is not supported.
*
* @throws UnsupportedOperationException always
*/
@Override
public void clear() {
throw new UnsupportedOperationException("clear");
}
// CleanerImpl.run()==>清理器clean()-->清理器performCleanup()-->action.run()
@Override
protected void performCleanup() {
action.run();
}
}
/**
* Perform cleaning on an unreachable PhantomReference.
*/
/*
* Reference
* │
* PhantomReference Cleanable
* └──────┬────────┘
* PhantomCleanable
* │
* PhantomCleanableRef
*
* 虚引用清理器,此类的属性既是虚引用,又是清理器
*/
public static final class PhantomCleanableRef extends PhantomCleanable<Object> {
private final Runnable action;
/**
* Constructor for a phantom cleanable reference.
*
* @param obj the object to monitor
* @param cleaner the cleaner
* @param action the action Runnable
*/
// 向cleaner注册跟踪的对象obj和清理动作action
public PhantomCleanableRef(Object obj, Cleaner cleaner, Runnable action) {
super(obj, cleaner);
this.action = action;
}
/**
* Constructor used only for root of phantom cleanable list.
*/
PhantomCleanableRef() {
super();
this.action = null;
}
/**
* Prevent access to referent even when it is still alive.
*
* @throws UnsupportedOperationException always
*/
@Override
public Object get() {
throw new UnsupportedOperationException("get");
}
/**
* Direct clearing of the referent is not supported.
*
* @throws UnsupportedOperationException always
*/
// 清理虚引用追踪的对象的引用,由父类实现
@Override
public void clear() {
throw new UnsupportedOperationException("clear");
}
// CleanerImpl.run()==>清理器clean()-->清理器performCleanup()-->action.run()
@Override
protected void performCleanup() {
action.run();
}
}
/**
* A ThreadFactory for InnocuousThreads. The factory is a singleton.
*/
// 清理器内部默认实现的"无害"线程工厂,用于创建"无害"线程,并将清理服务绑定到此线程
static final class InnocuousThreadFactory implements ThreadFactory {
final static ThreadFactory factory = new InnocuousThreadFactory();
final AtomicInteger cleanerThreadNumber = new AtomicInteger(); // 实现原子计数
// 返回单例工厂对象
static ThreadFactory factory() {
return factory;
}
public Thread newThread(Runnable r) {
return AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public Thread run() {
Thread t = InnocuousThread.newThread(r);
t.setPriority(Thread.MAX_PRIORITY - 2);
t.setName("Cleaner-" + cleanerThreadNumber.getAndIncrement());
return t;
}
});
}
}
/**
* A PhantomCleanable implementation for tracking the Cleaner itself.
*/
// 创建清理的时候,在启动清理服务前,将清理器自身对象加入到追踪列表
static final class CleanerCleanable extends PhantomCleanable<Cleaner> {
CleanerCleanable(Cleaner cleaner) {
super(cleaner, cleaner);
}
// 默认情形下,清理器对象自身被回收时不执行回到动作
@Override
protected void performCleanup() {
// no action
}
}
}