![](/img/trans.png)
[英]what is meaning of “readlimit” parameter at mark method in java?
[英]What is the meaning of `int dummy` in the fillInStackTrace() method in Java?
private native Throwable fillInStackTrace(int dummy);
创建异常时,此方法以dummy=0
调用。 是什么意思dummy
? 是构建堆栈跟踪的深度吗?
更新:
public class MyEx extends RuntimeException{
@Override
public synchronized Throwable fillInStackTrace() {
Method method = null;
try {
Class<?>[] classArray = new Class<?>[1];
classArray[0] = int.class;
method =Throwable.class.getDeclaredMethod("fillInStackTrace", classArray);
method.setAccessible(true);
Object obg = method.invoke(this, 6);
StackTraceElement[] trace = ((MyEx) obg).getStackTrace();
System.out.println();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return this;
}
}
看来dummy真的是dummy,不管我放什么值,结果都是一样的……
我想将堆栈跟踪大小限制为 3 以消耗更少的内存,并且从执行的角度来看,创建异常也会更快。 我有一个真实的用例,我需要很多异常,但堆栈跟踪很浅。
这不代表什么。 这是一个伪论证。
本机代码实现完全忽略该参数。 例如,在 OpenJDK 11 中,桥接方法的实现如下:
/*
* Fill in the current stack trace in this exception. This is
* usually called automatically when the exception is created but it
* may also be called explicitly by the user. This routine returns
* `this' so you can write 'throw e.fillInStackTrace();'
*/
JNIEXPORT jobject JNICALL
Java_java_lang_Throwable_fillInStackTrace(JNIEnv *env,
jobject throwable, jint dummy)
{
JVM_FillInStackTrace(env, throwable);
return throwable;
}
如您所见, dummy
参数被忽略。
如果您正在寻找一种限制-XX:MaxJavaStackTraceDepth=depth
方法,支持的方法是使用-XX:MaxJavaStackTraceDepth=depth
选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.