Skip to content

Commit 8a3c100

Browse files
Stuart MarksRoger Riggs
Stuart Marks
and
Roger Riggs
committed
8344461: Additional cleanup in NewThreadAction
8344867: Cleanup unneeded qualified exports to java.rmi Co-authored-by: Roger Riggs <rriggs@openjdk.org> Reviewed-by: rriggs
1 parent f904480 commit 8a3c100

File tree

9 files changed

+107
-151
lines changed

9 files changed

+107
-151
lines changed

src/java.base/share/classes/module-info.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@
316316
exports sun.reflect.misc to
317317
java.desktop,
318318
java.management,
319-
java.rmi,
320319
java.sql.rowset;
321320
exports sun.security.internal.interfaces to
322321
jdk.crypto.cryptoki;
@@ -329,7 +328,6 @@
329328
exports sun.security.pkcs to
330329
jdk.jartool;
331330
exports sun.security.provider to
332-
java.rmi,
333331
java.security.jgss,
334332
jdk.crypto.cryptoki,
335333
jdk.security.auth;
@@ -344,7 +342,6 @@
344342
jdk.jartool;
345343
exports sun.security.util to
346344
java.naming,
347-
java.rmi,
348345
java.security.jgss,
349346
java.security.sasl,
350347
java.smartcardio,

src/java.rmi/share/classes/sun/rmi/runtime/NewThreadAction.java

Lines changed: 0 additions & 127 deletions
This file was deleted.

src/java.rmi/share/classes/sun/rmi/runtime/RuntimeUtil.java

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,46 @@
3636
* There is a single instance of this class, which can be obtained
3737
* with a getInstance() call.
3838
*
39+
* This class also contains a couple static methods for creating
40+
* threads. The methods allow the choice of the Runnable for the
41+
* new thread to execute, the name of the new thread (which will
42+
* be prefixed with "RMI "), and whether or not it will be a daemon
43+
* thread.
44+
*
45+
* The new thread may be created in the system thread group (the root
46+
* of the thread group tree) or an internally created non-system
47+
* thread group (the "user" thread group).
48+
*
49+
* The new thread will have the system class loader as its initial
50+
* context class loader (that is, its context class loader will NOT be
51+
* inherited from the current thread).
52+
*
3953
* @author Peter Jones
4054
**/
4155
public final class RuntimeUtil {
4256

57+
/**
58+
* Cached reference to the system (root) thread group.
59+
*/
60+
private static final ThreadGroup systemThreadGroup;
61+
static {
62+
ThreadGroup group = Thread.currentThread().getThreadGroup();
63+
ThreadGroup parent;
64+
while ((parent = group.getParent()) != null) {
65+
group = parent;
66+
}
67+
systemThreadGroup = group;
68+
}
69+
70+
/**
71+
* Special child of the system thread group for running tasks that
72+
* may execute user code. The need for a separate thread group may
73+
* be a vestige of it having had a different security policy from
74+
* the system thread group, so this might no longer be necessary.
75+
*/
76+
private static final ThreadGroup userThreadGroup =
77+
new ThreadGroup(systemThreadGroup, "RMI Runtime");
78+
4379
/** runtime package log */
4480
private static final Log runtimeLog =
4581
Log.getLog("sun.rmi.runtime", null, false);
@@ -54,16 +90,25 @@ public final class RuntimeUtil {
5490
/** thread pool for scheduling delayed tasks */
5591
private final ScheduledThreadPoolExecutor scheduler;
5692

93+
/**
94+
* Creates the single instance of RuntimeUtil. Note that this is called
95+
* from a static initializer, and it has a ThreadFactory that calls
96+
* static methods on this class, possibly from other threads. This
97+
* should be ok, as the ScheduledThreadPoolExecutor constructor
98+
* returns immediately without blocking on the creation of threads
99+
* by the factory.
100+
*/
57101
private RuntimeUtil() {
58102
scheduler = new ScheduledThreadPoolExecutor(
59103
schedulerThreads,
60104
new ThreadFactory() {
61105
private final AtomicInteger count = new AtomicInteger();
62106
public Thread newThread(Runnable runnable) {
63107
try {
64-
return new NewThreadAction(runnable,
108+
return newSystemThread(
109+
runnable,
65110
"Scheduler(" + count.getAndIncrement() + ")",
66-
true).run();
111+
true);
67112
} catch (Throwable t) {
68113
runtimeLog.log(Level.WARNING,
69114
"scheduler thread factory throws", t);
@@ -93,4 +138,48 @@ public static RuntimeUtil getInstance() {
93138
public ScheduledThreadPoolExecutor getScheduler() {
94139
return scheduler;
95140
}
141+
142+
// Thread creation methods.
143+
144+
/**
145+
* Internal method to create a new thread with the given settings.
146+
*
147+
* @param group the thread group, should be systemThreadGroup or userThreadGroup
148+
* @param runnable the thread's task
149+
* @param name the thread's name, which will be prefixed with "RMI "
150+
* @param daemon whether the thread should be a daemon
151+
* @return the newly created thread
152+
*/
153+
private static Thread newThread(ThreadGroup group, Runnable runnable, String name, boolean daemon) {
154+
Thread t = new Thread(group, runnable, "RMI " + name);
155+
t.setContextClassLoader(ClassLoader.getSystemClassLoader());
156+
t.setDaemon(daemon);
157+
return t;
158+
}
159+
160+
/**
161+
* Creates and returns, but does not start, a new thread with the given settings.
162+
* The thread will be in the system ("root") thread group.
163+
*
164+
* @param runnable the thread's task
165+
* @param name the thread's name, which will be prefixed with "RMI "
166+
* @param daemon whether the thread should be a daemon
167+
* @return the newly created thread
168+
*/
169+
public static Thread newSystemThread(Runnable runnable, String name, boolean daemon) {
170+
return newThread(systemThreadGroup, runnable, name, daemon);
171+
}
172+
173+
/**
174+
* Creates and returns, but does not start, a new thread with the given settings.
175+
* The thread will be in the RMI user thread group.
176+
*
177+
* @param runnable the thread's task
178+
* @param name the thread's name, which will be prefixed with "RMI "
179+
* @param daemon whether the thread should be a daemon
180+
* @return the newly created thread
181+
*/
182+
public static Thread newUserThread(Runnable runnable, String name, boolean daemon) {
183+
return newThread(userThreadGroup, runnable, name, daemon);
184+
}
96185
}

src/java.rmi/share/classes/sun/rmi/transport/DGCClient.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import java.rmi.server.ObjID;
4242

4343
import sun.rmi.runtime.Log;
44-
import sun.rmi.runtime.NewThreadAction;
44+
import sun.rmi.runtime.RuntimeUtil;
4545
import sun.rmi.server.UnicastRef;
4646
import sun.rmi.server.Util;
4747

@@ -237,8 +237,7 @@ private EndpointEntry(final Endpoint endpoint) {
237237
throw new Error("internal error creating DGC stub");
238238
}
239239
renewCleanThread =
240-
new NewThreadAction(new RenewCleanThread(),
241-
"RenewClean-" + endpoint, true).run();
240+
RuntimeUtil.newSystemThread(new RenewCleanThread(), "RenewClean-" + endpoint, true);
242241
renewCleanThread.start();
243242
}
244243

src/java.rmi/share/classes/sun/rmi/transport/ObjectTable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import java.util.HashMap;
3434
import java.util.Map;
3535
import sun.rmi.runtime.Log;
36-
import sun.rmi.runtime.NewThreadAction;
36+
import sun.rmi.runtime.RuntimeUtil;
3737

3838
/**
3939
* Object table shared by all implementors of the Transport interface.
@@ -271,7 +271,7 @@ static void incrementKeepAliveCount() {
271271
keepAliveCount++;
272272

273273
if (reaper == null) {
274-
reaper = new NewThreadAction(new Reaper(), "Reaper", false).run();
274+
reaper = RuntimeUtil.newSystemThread(new Reaper(), "Reaper", false);
275275
reaper.start();
276276
}
277277

src/java.rmi/share/classes/sun/rmi/transport/Target.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.rmi.server.Unreferenced;
3232
import java.util.*;
3333
import sun.rmi.runtime.Log;
34-
import sun.rmi.runtime.NewThreadAction;
34+
import sun.rmi.runtime.RuntimeUtil;
3535
import sun.rmi.server.Dispatcher;
3636

3737
/**
@@ -312,10 +312,10 @@ private synchronized void refSetRemove(VMID vmid) {
312312
*/
313313
Remote obj = getImpl();
314314
if (obj instanceof Unreferenced unrefObj) {
315-
new NewThreadAction(() -> {
315+
RuntimeUtil.newUserThread(() -> {
316316
Thread.currentThread().setContextClassLoader(ccl);
317317
unrefObj.unreferenced();
318-
}, "Unreferenced-" + nextThreadNum++, false, true).run().start();
318+
}, "Unreferenced-" + nextThreadNum++, false).start();
319319
// REMIND: access to nextThreadNum not synchronized; you care?
320320
}
321321

src/java.rmi/share/classes/sun/rmi/transport/tcp/TCPChannel.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.util.concurrent.ScheduledExecutorService;
3838
import java.util.concurrent.TimeUnit;
3939
import sun.rmi.runtime.Log;
40-
import sun.rmi.runtime.NewThreadAction;
4140
import sun.rmi.runtime.RuntimeUtil;
4241
import sun.rmi.transport.Channel;
4342
import sun.rmi.transport.Connection;
@@ -401,9 +400,9 @@ public ConnectionAcceptor(TCPTransport transport) {
401400
* Start a new thread to accept connections.
402401
*/
403402
public void startNewAcceptor() {
404-
Thread t = new NewThreadAction(ConnectionAcceptor.this,
405-
"TCPChannel Accept-" + ++ threadNum,
406-
true).run();
403+
Thread t = RuntimeUtil.newSystemThread(ConnectionAcceptor.this,
404+
"TCPChannel Accept-" + ++ threadNum,
405+
true);
407406
t.start();
408407
}
409408

src/java.rmi/share/classes/sun/rmi/transport/tcp/TCPEndpoint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import java.util.Map;
4646
import java.util.Set;
4747
import sun.rmi.runtime.Log;
48-
import sun.rmi.runtime.NewThreadAction;
48+
import sun.rmi.runtime.RuntimeUtil;
4949
import sun.rmi.transport.Channel;
5050
import sun.rmi.transport.Endpoint;
5151
import sun.rmi.transport.Target;
@@ -752,7 +752,7 @@ static String attemptFQDN(InetAddress localAddr)
752752
private void getFQDN() {
753753

754754
/* FQDN finder will run in RMI threadgroup. */
755-
Thread t = new NewThreadAction(FQDN.this, "FQDN Finder", true).run();
755+
Thread t = RuntimeUtil.newSystemThread(FQDN.this, "FQDN Finder", true);
756756
t.start();
757757
}
758758

0 commit comments

Comments
 (0)