Skip to content
Permalink
Browse files
8261036: Reduce classes loaded by CleanerFactory initialization
Reviewed-by: rriggs
  • Loading branch information
cl4es committed Feb 4, 2021
1 parent e8ad8b3 commit 992b50087d2ec8878dfcbbd1820a00b6b6bdf644
@@ -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 @@ private static String newName() {
}

/**
* 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 static Thread newThread(Runnable target) {
* 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 static Thread newSystemThread(Runnable target) {
* 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 void run() {
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);
}
@@ -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 @@
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 @@
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);
}
});

@@ -213,15 +213,8 @@ static ThreadFactory factory() {
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);
}
}

0 comments on commit 992b500

Please sign in to comment.