---
title: "java  singalException"
author: "dikar"
date: "2012-11-12T21:39:42Z"
tags: []
categories: ["开源软件"]
slug: "java-singalexception"
draft: false
lastmod: "2012-11-12T21:41:57Z"
id: 1724238
url: "https://dikar.iteye.com/blog/1724238"
---

之前看到毕大师一封关于异常多造成的cpu us很高的分析邮件，同时今天看到有人做了异常的benchmark，又加上经常遇到某些需求是用异常来阻断程序的。java中据我所知的情况下，目前只有用异常来阻断程序执行，没有其他高效的阻断程序执行的方式，故如果需要类似singal方式来阻断程序执行的，同时又不是当作异常来处理的，那么最好是重载throwable的如下方法：

 

```
/**
     * 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.
     *
     * @return  a reference to this <code>Throwable</code> instance.
     * @see     java.lang.Throwable#printStackTrace()
     */
    public synchronized native Throwable fillInStackTrace();
```

```
public  Throwable fillInStackTrace()｛  return this;｝
```

 这样在这个异常被new时就不会有比较高的开销，按照我的命名习惯这种异常类应该叫SingalException 。

 

附上别人做的benchmark

 

Java基本操作性能

[https://github.com/wenshao/benchmark/wiki](https://github.com/wenshao/benchmark/wiki)