Skip to content

Commit

Permalink
8261036: Reduce classes loaded by CleanerFactory initialization
Browse files Browse the repository at this point in the history
Backport-of: 992b500
  • Loading branch information
TheRealMDoerr committed Sep 16, 2021
1 parent 25cad2f commit 295f882
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 deletions.
61 changes: 46 additions & 15 deletions src/java.base/share/classes/jdk/internal/misc/InnocuousThread.java
@@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
});
}
Expand All @@ -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.putObjectRelease(this, INHERITEDACCESSCONTROLCONTEXT, ACC);
Expand Down Expand Up @@ -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);
}
Expand Down
14 changes: 3 additions & 11 deletions src/java.base/share/classes/jdk/internal/ref/CleanerFactory.java
@@ -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
Expand Down Expand Up @@ -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;

/**
Expand All @@ -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);
}
});

Expand Down
11 changes: 2 additions & 9 deletions src/java.base/share/classes/jdk/internal/ref/CleanerImpl.java
Expand Up @@ -324,15 +324,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);
}
}

Expand Down

1 comment on commit 295f882

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.