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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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. * and its context class loader is set to the system class loader.
*/ */
public static Thread newThread(Runnable target) { public static Thread newThread(Runnable target) {
@ -62,14 +62,22 @@ public final class InnocuousThread extends Thread {
* set to the system class loader. * set to the system class loader.
*/ */
public static Thread newThread(String name, Runnable target) { 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( return AccessController.doPrivileged(
new PrivilegedAction<Thread>() { new PrivilegedAction<Thread>() {
@Override @Override
public Thread run() { public Thread run() {
return new InnocuousThread(INNOCUOUSTHREADGROUP, return createThread(name, target, ClassLoader.getSystemClassLoader(), priority);
target,
name,
ClassLoader.getSystemClassLoader());
} }
}); });
} }
@ -86,16 +94,35 @@ public final class InnocuousThread extends Thread {
* Returns a new InnocuousThread with null context class loader. * Returns a new InnocuousThread with null context class loader.
*/ */
public static Thread newSystemThread(String name, Runnable target) { 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( return AccessController.doPrivileged(
new PrivilegedAction<Thread>() { new PrivilegedAction<Thread>() {
@Override @Override
public Thread run() { public Thread run() {
return new InnocuousThread(INNOCUOUSTHREADGROUP, return createThread(name, target, null, priority);
target, name, null);
} }
}); });
} }
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) { private InnocuousThread(ThreadGroup group, Runnable target, String name, ClassLoader tccl) {
super(group, target, name, 0L, false); super(group, target, name, 0L, false);
UNSAFE.putReferenceRelease(this, INHERITEDACCESSCONTROLCONTEXT, ACC); UNSAFE.putReferenceRelease(this, INHERITEDACCESSCONTROLCONTEXT, ACC);
@ -167,13 +194,17 @@ public final class InnocuousThread extends Thread {
group = parent; group = parent;
} }
final ThreadGroup root = group; final ThreadGroup root = group;
INNOCUOUSTHREADGROUP = AccessController.doPrivileged( if (System.getSecurityManager() == null) {
new PrivilegedAction<ThreadGroup>() { INNOCUOUSTHREADGROUP = new ThreadGroup(root, "InnocuousThreadGroup");
@Override } else {
public ThreadGroup run() { INNOCUOUSTHREADGROUP = AccessController.doPrivileged(
return new ThreadGroup(root, "InnocuousThreadGroup"); new PrivilegedAction<ThreadGroup>() {
} @Override
}); public ThreadGroup run() {
return new ThreadGroup(root, "InnocuousThreadGroup");
}
});
}
} catch (Exception e) { } catch (Exception e) {
throw new Error(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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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 jdk.internal.misc.InnocuousThread;
import java.lang.ref.Cleaner; import java.lang.ref.Cleaner;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
/** /**
@ -42,14 +40,8 @@ public final class CleanerFactory {
private final static Cleaner commonCleaner = Cleaner.create(new ThreadFactory() { private final static Cleaner commonCleaner = Cleaner.create(new ThreadFactory() {
@Override @Override
public Thread newThread(Runnable r) { public Thread newThread(Runnable r) {
return AccessController.doPrivileged(new PrivilegedAction<>() { return InnocuousThread.newSystemThread("Common-Cleaner",
@Override r, Thread.MAX_PRIORITY - 2);
public Thread run() {
Thread t = InnocuousThread.newSystemThread("Common-Cleaner", r);
t.setPriority(Thread.MAX_PRIORITY - 2);
return t;
}
});
} }
}); });

View File

@ -213,15 +213,8 @@ public final class CleanerImpl implements Runnable {
final AtomicInteger cleanerThreadNumber = new AtomicInteger(); final AtomicInteger cleanerThreadNumber = new AtomicInteger();
public Thread newThread(Runnable r) { public Thread newThread(Runnable r) {
return AccessController.doPrivileged(new PrivilegedAction<>() { return InnocuousThread.newThread("Cleaner-" + cleanerThreadNumber.getAndIncrement(),
@Override r, Thread.MIN_PRIORITY - 2);
public Thread run() {
Thread t = InnocuousThread.newThread(r);
t.setPriority(Thread.MAX_PRIORITY - 2);
t.setName("Cleaner-" + cleanerThreadNumber.getAndIncrement());
return t;
}
});
} }
} }