forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInnocuousThread.java
188 lines (162 loc) · 7.55 KB
/
InnocuousThread.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
/*
* Copyright (c) 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.misc;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.concurrent.atomic.AtomicInteger;
/**
* A thread that has no permissions, is not a member of any user-defined
* ThreadGroup and supports the ability to erase ThreadLocals.
*/
// "无害"线程,系统内部使用,用完之后会清理ThreadLocal信息
public final class InnocuousThread extends Thread {
private static final long THREAD_LOCALS; // 获取Thread中threadLocals字段的地址
private static final long INHERITABLE_THREAD_LOCALS; // 获取Thread中inheritableThreadLocals字段的地址
private static final long INHERITEDACCESSCONTROLCONTEXT; // 获取Thread中inheritedAccessControlContext字段的地址
private static final long CONTEXTCLASSLOADER; // 获取Thread中contextClassLoader字段的地址
private static final jdk.internal.misc.Unsafe UNSAFE;
private static final AccessControlContext ACC;
private static final ThreadGroup INNOCUOUSTHREADGROUP;
// 原子计数器
private static final AtomicInteger threadNumber = new AtomicInteger(1);
// ensure run method is run only once
private volatile boolean hasRun; // 确保run方法只运行一次
// Use Unsafe to access Thread group and ThreadGroup parent fields
static {
try {
ACC = new AccessControlContext(new ProtectionDomain[]{new ProtectionDomain(null, null)});
UNSAFE = jdk.internal.misc.Unsafe.getUnsafe();
Class<?> tk = Thread.class;
Class<?> gk = ThreadGroup.class;
THREAD_LOCALS = UNSAFE.objectFieldOffset(tk, "threadLocals");
INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset(tk, "inheritableThreadLocals");
INHERITEDACCESSCONTROLCONTEXT = UNSAFE.objectFieldOffset(tk, "inheritedAccessControlContext");
CONTEXTCLASSLOADER = UNSAFE.objectFieldOffset(tk, "contextClassLoader");
long tg = UNSAFE.objectFieldOffset(tk, "group");
long gp = UNSAFE.objectFieldOffset(gk, "parent");
// 存储根线程组
ThreadGroup group = (ThreadGroup) UNSAFE.getObject(Thread.currentThread(), tg);
// Find and use topmost ThreadGroup as parent of new group
while(group != null) {
ThreadGroup parent = (ThreadGroup) UNSAFE.getObject(group, gp);
if(parent == null)
break;
group = parent;
}
final ThreadGroup root = group;
INNOCUOUSTHREADGROUP = AccessController.doPrivileged(new PrivilegedAction<ThreadGroup>() {
@Override
public ThreadGroup run() {
return new ThreadGroup(root, "InnocuousThreadGroup");
}
});
} catch(Exception e) {
throw new Error(e);
}
}
private InnocuousThread(ThreadGroup group, Runnable target, String name, ClassLoader tccl) {
super(group, target, name, 0L, false);
UNSAFE.putObjectRelease(this, INHERITEDACCESSCONTROLCONTEXT, ACC);
UNSAFE.putObjectRelease(this, CONTEXTCLASSLOADER, tccl);
}
/*
* Returns a new InnocuousThread with an auto-generated thread name and its context class loader is set to the system class loader.
*
* 返回一个新的"无害"线程(InnocuousThread)。该线程的名称是自动生成的,其类加载器被设置为AppClassLoader。
*/
public static Thread newThread(Runnable target) {
return newThread(newName(), target);
}
/*
* Returns a new InnocuousThread with its context class loader set to the system class loader.
*
* 返回一个新的"无害"线程(InnocuousThread),其类加载器被设置为AppClassLoader。
*/
public static Thread newThread(String name, Runnable target) {
return AccessController.doPrivileged(new PrivilegedAction<Thread>() {
@Override
public Thread run() {
return new InnocuousThread(INNOCUOUSTHREADGROUP, target, name, ClassLoader.getSystemClassLoader());
}
});
}
/**
* Returns a new InnocuousThread with an auto-generated thread name. Its context class loader is set to null.
*
* 返回一个新的"无害"线程(InnocuousThread)。该线程的名称是自动生成的,其类加载器被设置为null。
*/
public static Thread newSystemThread(Runnable target) {
return newSystemThread(newName(), target);
}
/*
* Returns a new InnocuousThread with null context class loader.
*
* 返回一个新的"无害"线程(InnocuousThread),其类加载器被设置为null。
*/
public static Thread newSystemThread(String name, Runnable target) {
return AccessController.doPrivileged(new PrivilegedAction<Thread>() {
@Override
public Thread run() {
return new InnocuousThread(INNOCUOUSTHREADGROUP, target, name, null);
}
});
}
// 自动生成线程名称
private static String newName() {
return "InnocuousThread-" + threadNumber.getAndIncrement();
}
/*
* Drops all thread locals (and inherited thread locals).
*
* 删除Thread中的threadLocals和inheritableThreadLocals。
* 即清除该线程中所有ThreadLocal信息。
*/
public final void eraseThreadLocals() {
UNSAFE.putObject(this, THREAD_LOCALS, null);
UNSAFE.putObject(this, INHERITABLE_THREAD_LOCALS, null);
}
@Override
public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) {
// silently fail
}
@Override
public void setContextClassLoader(ClassLoader cl) {
// Allow clearing of the TCCL to remove the reference to the system classloader.
if(cl == null)
super.setContextClassLoader(null);
else
throw new SecurityException("setContextClassLoader");
}
// 执行线程的动作(只执行一次)
@Override
public void run() {
if(Thread.currentThread() == this && !hasRun) {
hasRun = true;
super.run();
}
}
}