java.lang.ThreadLocal
,而是使用扩展它的内部类,由
new ThreadLocal<Boolean>() {
@Override protected Boolean initialValue() {
return Boolean.FALSE;
}
};
通过解决此问题
DefineClass
ClassLoader
这会根据需要返回类。
private static ThreadLocal<Boolean> inInstrumentMethod
= ThreadLocal.withInitial(() -> Boolean.FALSE);
如果您使用的是Java8之前的版本,那么您不能这样使用它,因此在这种情况下,最好的解决方案是重写代码以接受
null
作为初始值,无需指定不同的初始值:
private static ThreadLocal<Boolean> inInstrumentMethod = new ThreadLocal<>();
public static void onEntry(int methodID)
{
if (inInstrumentMethod.get()!=null) {
return;
} else {
inInstrumentMethod.set(true);
}
System.out.println("Method ID: " + methodID);
inInstrumentMethod.set(null);
}
ThreadLocal