Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8261036: Reduce classes loaded by CleanerFactory initialization #353

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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.putObjectRelease(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 @@ 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);
}
});

@@ -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);
}
}