8261036: Reduce classes loaded by CleanerFactory initialization

Reviewed-by: rriggs
This commit is contained in:
Claes Redestad 2021-02-04 10:46:10 +00:00
parent e8ad8b3504
commit 992b50087d
3 changed files with 51 additions and 35 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021, 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
@ -50,7 +50,7 @@ public final class InnocuousThread extends Thread {
}
/**
* Returns a new InnocuousThread with an auto-generated thread name
* Returns a new InnocuousThread with an auto-generated thread name,
* and its context class loader is set to the system class loader.
*/
public static Thread newThread(Runnable target) {
@ -62,14 +62,22 @@ public final class InnocuousThread extends Thread {
* set to the system class loader.
*/
public static Thread newThread(String name, Runnable target) {
return newThread(name, target, -1);
}
/**
* Returns a new InnocuousThread with its context class loader
* set to the system class loader. The thread priority will be
* set to the given priority.
*/
public static Thread newThread(String name, Runnable target, int priority) {
if (System.getSecurityManager() == null) {
return createThread(name, target, ClassLoader.getSystemClassLoader(), priority);
}
return AccessController.doPrivileged(
new PrivilegedAction<Thread>() {
@Override
public Thread run() {
return new InnocuousThread(INNOCUOUSTHREADGROUP,
target,
name,
ClassLoader.getSystemClassLoader());
return createThread(name, target, ClassLoader.getSystemClassLoader(), priority);
}
});
}
@ -86,16 +94,35 @@ public final class InnocuousThread extends Thread {
* Returns a new InnocuousThread with null context class loader.
*/
public static Thread newSystemThread(String name, Runnable target) {
return newSystemThread(name, target, -1);
}
/**
* Returns a new InnocuousThread with null context class loader.
* Thread priority is set to the given priority.
*/
public static Thread newSystemThread(String name, Runnable target, int priority) {
if (System.getSecurityManager() == null) {
return createThread(name, target, null, priority);
}
return AccessController.doPrivileged(
new PrivilegedAction<Thread>() {
@Override
public Thread run() {
return new InnocuousThread(INNOCUOUSTHREADGROUP,
target, name, null);
return createThread(name, target, null, priority);
}
});
}
private static Thread createThread(String name, Runnable target, ClassLoader loader, int priority) {
Thread t = new InnocuousThread(INNOCUOUSTHREADGROUP,
target, name, loader);
if (priority >= 0) {
t.setPriority(priority);
}
return t;
}
private InnocuousThread(ThreadGroup group, Runnable target, String name, ClassLoader tccl) {
super(group, target, name, 0L, false);
UNSAFE.putReferenceRelease(this, INHERITEDACCESSCONTROLCONTEXT, ACC);
@ -167,13 +194,17 @@ public final class InnocuousThread extends Thread {
group = parent;
}
final ThreadGroup root = group;
INNOCUOUSTHREADGROUP = AccessController.doPrivileged(
new PrivilegedAction<ThreadGroup>() {
@Override
public ThreadGroup run() {
return new ThreadGroup(root, "InnocuousThreadGroup");
}
});
if (System.getSecurityManager() == null) {
INNOCUOUSTHREADGROUP = new ThreadGroup(root, "InnocuousThreadGroup");
} else {
INNOCUOUSTHREADGROUP = AccessController.doPrivileged(
new PrivilegedAction<ThreadGroup>() {
@Override
public ThreadGroup run() {
return new ThreadGroup(root, "InnocuousThreadGroup");
}
});
}
} catch (Exception e) {
throw new Error(e);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, 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
@ -28,8 +28,6 @@ package jdk.internal.ref;
import jdk.internal.misc.InnocuousThread;
import java.lang.ref.Cleaner;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.ThreadFactory;
/**
@ -42,14 +40,8 @@ public final class CleanerFactory {
private final static Cleaner commonCleaner = Cleaner.create(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public Thread run() {
Thread t = InnocuousThread.newSystemThread("Common-Cleaner", r);
t.setPriority(Thread.MAX_PRIORITY - 2);
return t;
}
});
return InnocuousThread.newSystemThread("Common-Cleaner",
r, Thread.MAX_PRIORITY - 2);
}
});

View File

@ -213,15 +213,8 @@ public final class CleanerImpl implements Runnable {
final AtomicInteger cleanerThreadNumber = new AtomicInteger();
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;
}
});
return InnocuousThread.newThread("Cleaner-" + cleanerThreadNumber.getAndIncrement(),
r, Thread.MIN_PRIORITY - 2);
}
}