在Java中,异常还有一个颇有用的用途,就是终止方法的执行,在Play framework这个牛B的框架中,mvc部分就是使用抛出异常的方式终止方法的执行,最终达到response的目的。html
但咱们都知道JVM在构造异常对象的时候是很是耗时的,据有些蛋疼的人测试,构造一个异常对象要比构造正常的java对象更加的耗费时间(我没有测试过)。java
还好,如今咱们能够使用Fast Exceptions!在Play framework最新的版本中就使用了此种Exception。mvc
其实木有什么神秘的,只是override java异常的 public Throwable fillInStackTrace()方法便可:框架
/** * Since we override this method, no stacktrace is generated - much faster * @return always null */ public Throwable fillInStackTrace() { return null; }
关于Fash Excepiton的测试:ide
http://www.javaspecialists.eu/archive/Issue129.html性能
异常性能是很差,根本缘由在于:
异常基类Throwable.java的public synchronized native Throwable fillInStackTrace()方法
方法介绍:
Fills in the execution stack trace. This method records within this Throwable object information about the current state of the stack frames for the current thread.
性能开销在于:
1. 是一个synchronized方法(主因)
2. 须要填充线程运行堆栈信息测试
复写他就行了。this