Skip to content

Commit

Permalink
fix classloader dead lock in jdk7+ - 5.x (#2018)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanCao authored and wu-sheng committed Dec 7, 2018
1 parent 69a765d commit d494457
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
Expand All @@ -45,6 +46,11 @@
* @author wusheng
*/
public class AgentClassLoader extends ClassLoader {

static {
tryRegisterAsParallelCapable();
}

private static final ILog logger = LogManager.getLogger(AgentClassLoader.class);
/**
* The default class loader for the agent.
Expand All @@ -55,6 +61,28 @@ public class AgentClassLoader extends ClassLoader {
private List<Jar> allJars;
private ReentrantLock jarScanLock = new ReentrantLock();

/**
* Functional Description: solve the classloader dead lock when jvm start
* only support JDK7+, since ParallelCapable appears in JDK7+
*/
private static void tryRegisterAsParallelCapable() {
Method[] methods = ClassLoader.class.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
String methodName = method.getName();
if ("registerAsParallelCapable".equalsIgnoreCase(methodName)) {
try {
method.setAccessible(true);
method.invoke(null);
} catch (Exception e) {
logger.warn(e, "can not invoke ClassLoader.registerAsParallelCapable()");
}
return;
}
}
}


public static AgentClassLoader getDefault() {
return DEFAULT_LOADER;
}
Expand Down

0 comments on commit d494457

Please sign in to comment.