Throwable.fillInStackTrace

本文详细介绍了覆写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
发生watchdog重启时,主进程下线程在等锁,持锁线程堆栈为"AlarmManager" prio=5 tid=106 Runnable | group=“main” sCount=0 ucsCount=0 flags=0 obj=0x3a5aac0 self=0xb400007bf44c2800 | sysTid=2252 nice=0 cgrp=ssfg sched=1073741824/0 handle=0x7bc6798500 | state=R schedstat=( 322197214360 1342951389650 610847 ) utm=9613 stm=22606 core=7 HZ=100 | stack=0x7bc6695000-0x7bc6697000 stackSize=1037KB | held mutexes= “mutator lock”(shared held) native: #00 pc 004ca2ec /apex/com.android.art/lib64/libart.so (art::DumpNativeStack+108) (BuildId: 80d2ab18f9d259d8e546c1e6bae752b1) native: #01 pc 004342b4 /apex/com.android.art/lib64/libart.so (art::Thread::DumpStack const+436) (BuildId: 80d2ab18f9d259d8e546c1e6bae752b1) native: #02 pc 00433fd0 /apex/com.android.art/lib64/libart.so (art::DumpCheckpoint::Run+120) (BuildId: 80d2ab18f9d259d8e546c1e6bae752b1) native: #03 pc 0028e190 /apex/com.android.art/lib64/libart.so (art::Thread::RunCheckpointFunction+144) (BuildId: 80d2ab18f9d259d8e546c1e6bae752b1) native: #04 pc 0042bdac /apex/com.android.art/lib64/libart.so (artTestSuspendFromCode+256) (BuildId: 80d2ab18f9d259d8e546c1e6bae752b1) native: #05 pc 00343eec /apex/com.android.art/lib64/libart.so (art_quick_test_suspend+156) (BuildId: 80d2ab18f9d259d8e546c1e6bae752b1) at java.lang.Throwable.nativeFillInStackTrace(Native method) at java.lang.Throwable.fillInStackTrace(Throwable.java:819) locked <@addr=0xb8b2788> (a android.util.Log$TerribleFailure) at java.lang.Throwable.(Throwable.java:308) at java.lang.Exception.(Exception.java:85) at android.util.Log$TerribleFailure.(Log.java:120) at android.util.Log.wtf(Log.java:351) at android.util.Slog.wtf(Slog.java:221) at com.android.server.alarm.AlarmManagerService.decrementAlarmCount(AlarmManagerService.java:6112) at com.android.server.alarm.AlarmManagerService.deliverAlarmsLocked(AlarmManagerService.java:4764) at com.android.server.alarm.AlarmManagerService$AlarmThread.run(AlarmManagerService.java:5072) locked <0x047b7290> (a java.lang.Object) DumpLatencyMs: 2293.18 此时系统高负载,可以分析出什么
最新发布
12-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值