1
1
/*
2
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -50,7 +50,7 @@ private static String newName() {
50
50
}
51
51
52
52
/**
53
- * Returns a new InnocuousThread with an auto-generated thread name
53
+ * Returns a new InnocuousThread with an auto-generated thread name,
54
54
* and its context class loader is set to the system class loader.
55
55
*/
56
56
public static Thread newThread (Runnable target ) {
@@ -62,14 +62,22 @@ public static Thread newThread(Runnable target) {
62
62
* set to the system class loader.
63
63
*/
64
64
public static Thread newThread (String name , Runnable target ) {
65
+ return newThread (name , target , -1 );
66
+ }
67
+ /**
68
+ * Returns a new InnocuousThread with its context class loader
69
+ * set to the system class loader. The thread priority will be
70
+ * set to the given priority.
71
+ */
72
+ public static Thread newThread (String name , Runnable target , int priority ) {
73
+ if (System .getSecurityManager () == null ) {
74
+ return createThread (name , target , ClassLoader .getSystemClassLoader (), priority );
75
+ }
65
76
return AccessController .doPrivileged (
66
77
new PrivilegedAction <Thread >() {
67
78
@ Override
68
79
public Thread run () {
69
- return new InnocuousThread (INNOCUOUSTHREADGROUP ,
70
- target ,
71
- name ,
72
- ClassLoader .getSystemClassLoader ());
80
+ return createThread (name , target , ClassLoader .getSystemClassLoader (), priority );
73
81
}
74
82
});
75
83
}
@@ -86,16 +94,35 @@ public static Thread newSystemThread(Runnable target) {
86
94
* Returns a new InnocuousThread with null context class loader.
87
95
*/
88
96
public static Thread newSystemThread (String name , Runnable target ) {
97
+ return newSystemThread (name , target , -1 );
98
+ }
99
+
100
+ /**
101
+ * Returns a new InnocuousThread with null context class loader.
102
+ * Thread priority is set to the given priority.
103
+ */
104
+ public static Thread newSystemThread (String name , Runnable target , int priority ) {
105
+ if (System .getSecurityManager () == null ) {
106
+ return createThread (name , target , null , priority );
107
+ }
89
108
return AccessController .doPrivileged (
90
109
new PrivilegedAction <Thread >() {
91
110
@ Override
92
111
public Thread run () {
93
- return new InnocuousThread (INNOCUOUSTHREADGROUP ,
94
- target , name , null );
112
+ return createThread (name , target , null , priority );
95
113
}
96
114
});
97
115
}
98
116
117
+ private static Thread createThread (String name , Runnable target , ClassLoader loader , int priority ) {
118
+ Thread t = new InnocuousThread (INNOCUOUSTHREADGROUP ,
119
+ target , name , loader );
120
+ if (priority >= 0 ) {
121
+ t .setPriority (priority );
122
+ }
123
+ return t ;
124
+ }
125
+
99
126
private InnocuousThread (ThreadGroup group , Runnable target , String name , ClassLoader tccl ) {
100
127
super (group , target , name , 0L , false );
101
128
UNSAFE .putObjectRelease (this , INHERITEDACCESSCONTROLCONTEXT , ACC );
@@ -167,13 +194,17 @@ public void run() {
167
194
group = parent ;
168
195
}
169
196
final ThreadGroup root = group ;
170
- INNOCUOUSTHREADGROUP = AccessController .doPrivileged (
171
- new PrivilegedAction <ThreadGroup >() {
172
- @ Override
173
- public ThreadGroup run () {
174
- return new ThreadGroup (root , "InnocuousThreadGroup" );
175
- }
176
- });
197
+ if (System .getSecurityManager () == null ) {
198
+ INNOCUOUSTHREADGROUP = new ThreadGroup (root , "InnocuousThreadGroup" );
199
+ } else {
200
+ INNOCUOUSTHREADGROUP = AccessController .doPrivileged (
201
+ new PrivilegedAction <ThreadGroup >() {
202
+ @ Override
203
+ public ThreadGroup run () {
204
+ return new ThreadGroup (root , "InnocuousThreadGroup" );
205
+ }
206
+ });
207
+ }
177
208
} catch (Exception e ) {
178
209
throw new Error (e );
179
210
}
0 commit comments