Throwable.fillInStackTrace

Throwable.fillInStackTrace本是一个 synchronized native 方法,其如下: 

    /**
     * Fills in the execution stack trace. This method records within this
     * <code>Throwable</code> object information about the current state of
     * the stack frames for the current thread.
     *
     * 在 Throwable 对象中 填充 执行堆栈信息。此方法在 Throwable 对象中 记录 当前线程的栈帧的 当前状态信息
     *
     * @return  a reference to this <code>Throwable</code> instance.
     * @see     java.lang.Throwable#printStackTrace()
     */
    public synchronized native Throwable fillInStackTrace();

当抛出此异常时,就会通过这个方法填充当前线程的栈帧信息。如果,我们重新了此方法(native实现是将线程的栈帧信息记录到此 Throwable 对象中),不记录当前线程的栈帧信息,那么,当此异常被抛出时,就无法保存堆栈信息了
测试代码如下:

package marvin.doit.exception_cause_low_performance;

public class JustForDemoException extends RuntimeException {
	private static final long serialVersionUID = 2134186859756705446L;

	@Override
	public Throwable fillInStackTrace() {
		return this;
	}

	private static void method1() {
		System.out.println("in method 1");
		method2();
	}

	private static void method2() {
		System.out.println("in method 2");
		method3();
	}

	private static void method3() {
		System.out.println("in method 3");
		method4();
	}

	private static void method4() {
		System.out.println("in method 4");
		throw new JustForDemoException();
	}

	public static void main(String[] args) {
		method1();
	}

}

注意:上面代码中,我们覆写了 fillInStackTrace(),执行后,我们得到如下结果:


不要覆写(就想我们平时作的那样,平时谁会去覆写这个方法啊)。一切正常,我们又看到了常见的 堆栈信息




分析一下 "XNIO-2 task-52" #599 prio=5 os_prio=0 tid=0x000055eb63293800 nid=0x25d waiting for monitor entry [0x00007f6263877000] java.lang.Thread.State: BLOCKED (on object monitor) at java.lang.Throwable.fillInStackTrace(Native Method) at java.lang.Throwable.fillInStackTrace(Throwable.java:783) - locked <0x00000000eaac4a38> (a java.lang.reflect.InvocationTargetException) at java.lang.Throwable.<init>(Throwable.java:310) at java.lang.Exception.<init>(Exception.java:102) at java.lang.ReflectiveOperationException.<init>(ReflectiveOperationException.java:89) at java.lang.reflect.InvocationTargetException.<init>(InvocationTargetException.java:72) at sun.reflect.GeneratedMethodAccessor134.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426) at com.sun.proxy.$Proxy91.selectList(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223) at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForMany(MybatisMapperMethod.java:173) at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:78) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89) at com.sun.proxy.$Proxy185.selectList(Unknown Source) at sun.reflect.GeneratedMethodAccessor187.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
最新发布
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值