Skip to content

Commit 295f882

Browse files
committed
8261036: Reduce classes loaded by CleanerFactory initialization
Backport-of: 992b500
1 parent 25cad2f commit 295f882

File tree

3 files changed

+51
-35
lines changed

3 files changed

+51
-35
lines changed

src/java.base/share/classes/jdk/internal/misc/InnocuousThread.java

+46-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -50,7 +50,7 @@ private static String newName() {
5050
}
5151

5252
/**
53-
* Returns a new InnocuousThread with an auto-generated thread name
53+
* Returns a new InnocuousThread with an auto-generated thread name,
5454
* and its context class loader is set to the system class loader.
5555
*/
5656
public static Thread newThread(Runnable target) {
@@ -62,14 +62,22 @@ public static Thread newThread(Runnable target) {
6262
* set to the system class loader.
6363
*/
6464
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+
}
6576
return AccessController.doPrivileged(
6677
new PrivilegedAction<Thread>() {
6778
@Override
6879
public Thread run() {
69-
return new InnocuousThread(INNOCUOUSTHREADGROUP,
70-
target,
71-
name,
72-
ClassLoader.getSystemClassLoader());
80+
return createThread(name, target, ClassLoader.getSystemClassLoader(), priority);
7381
}
7482
});
7583
}
@@ -86,16 +94,35 @@ public static Thread newSystemThread(Runnable target) {
8694
* Returns a new InnocuousThread with null context class loader.
8795
*/
8896
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+
}
89108
return AccessController.doPrivileged(
90109
new PrivilegedAction<Thread>() {
91110
@Override
92111
public Thread run() {
93-
return new InnocuousThread(INNOCUOUSTHREADGROUP,
94-
target, name, null);
112+
return createThread(name, target, null, priority);
95113
}
96114
});
97115
}
98116

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+
99126
private InnocuousThread(ThreadGroup group, Runnable target, String name, ClassLoader tccl) {
100127
super(group, target, name, 0L, false);
101128
UNSAFE.putObjectRelease(this, INHERITEDACCESSCONTROLCONTEXT, ACC);
@@ -167,13 +194,17 @@ public void run() {
167194
group = parent;
168195
}
169196
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+
}
177208
} catch (Exception e) {
178209
throw new Error(e);
179210
}

src/java.base/share/classes/jdk/internal/ref/CleanerFactory.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,8 +28,6 @@
2828
import jdk.internal.misc.InnocuousThread;
2929

3030
import java.lang.ref.Cleaner;
31-
import java.security.AccessController;
32-
import java.security.PrivilegedAction;
3331
import java.util.concurrent.ThreadFactory;
3432

3533
/**
@@ -42,14 +40,8 @@ public final class CleanerFactory {
4240
private final static Cleaner commonCleaner = Cleaner.create(new ThreadFactory() {
4341
@Override
4442
public Thread newThread(Runnable r) {
45-
return AccessController.doPrivileged(new PrivilegedAction<>() {
46-
@Override
47-
public Thread run() {
48-
Thread t = InnocuousThread.newSystemThread("Common-Cleaner", r);
49-
t.setPriority(Thread.MAX_PRIORITY - 2);
50-
return t;
51-
}
52-
});
43+
return InnocuousThread.newSystemThread("Common-Cleaner",
44+
r, Thread.MAX_PRIORITY - 2);
5345
}
5446
});
5547

src/java.base/share/classes/jdk/internal/ref/CleanerImpl.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,8 @@ static ThreadFactory factory() {
324324
final AtomicInteger cleanerThreadNumber = new AtomicInteger();
325325

326326
public Thread newThread(Runnable r) {
327-
return AccessController.doPrivileged(new PrivilegedAction<>() {
328-
@Override
329-
public Thread run() {
330-
Thread t = InnocuousThread.newThread(r);
331-
t.setPriority(Thread.MAX_PRIORITY - 2);
332-
t.setName("Cleaner-" + cleanerThreadNumber.getAndIncrement());
333-
return t;
334-
}
335-
});
327+
return InnocuousThread.newThread("Cleaner-" + cleanerThreadNumber.getAndIncrement(),
328+
r, Thread.MIN_PRIORITY - 2);
336329
}
337330
}
338331

0 commit comments

Comments
 (0)