Skip to content

Commit

Permalink
Put other shutdown thread in own thread group (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehutch committed Oct 19, 2019
1 parent fceb181 commit e22b375
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/main/java/io/github/classgraph/ScanResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,16 @@ public void run() {
}
}

ThreadGroup parentThreadGroup = Thread.currentThread().getThreadGroup();
for (; parentThreadGroup.getParent() != null; parentThreadGroup = parentThreadGroup.getParent()) {
// Find system (toplevel) thread group, and use this as the parent thread group, in order to
// drop any references to the container's context classloader, so that it may be garbage collected
// if the application is unloaded (#376).
}
parentThreadGroup = new ThreadGroup(parentThreadGroup, "ClassGraph-shutdown-thread-group");

// Create shutdown hook thread to close all open/mapped DirectByteBuffers on shutdown
final Thread hookThread = new Thread() {
final Thread hookThread = new Thread(parentThreadGroup, new Runnable() {
@Override
public void run() {
// Close all ScanResult instances that have not yet been closed
Expand All @@ -253,7 +261,7 @@ public void run() {
}
}
}
};
}, "ClassGraph-shutdown-thread");
// Set shutdown hook thread classloader to system classloader, dropping ref to context classloader
hookThread.setContextClassLoader(systemClassLoader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ public Thread newThread(final Runnable runnable) {
// as the parent thread group, in order to drop any references to the container's context
// classloader, so that it may be garbage collected if the application is unloaded (#376).
}
parentThreadGroup = new ThreadGroup(parentThreadGroup, threadNamePrefix + "threadgroup");
parentThreadGroup = new ThreadGroup(parentThreadGroup, threadNamePrefix + "thread-group");
}
final Thread t = new Thread(parentThreadGroup, runnable, threadNamePrefix + threadIdx.getAndIncrement());
final Thread thread = new Thread(parentThreadGroup, runnable,
threadNamePrefix + threadIdx.getAndIncrement());
if (threadContextClassLoader != null) {
t.setContextClassLoader(threadContextClassLoader);
thread.setContextClassLoader(threadContextClassLoader);
}
t.setDaemon(daemon);
return t;
thread.setDaemon(daemon);
return thread;
}
}

0 comments on commit e22b375

Please sign in to comment.