可以使用daemon线程实现,因为daemon在没有其他线程运行后会自动结束。
Thread threadExpire = new Thread() { public void run() { try { sleep(expire * 1000); } catch (Throwable e) { Run.catchException(this, e); } finally { // 执行到这里肯定是超时了 Run.catchException( this, new Exception( obj.getClass() .getName() + " has expired,expire=" + expire)); System.exit(0); } } }; threadExpire.setDaemon(true); threadExpire.start();