Skip to content

Instantly share code, notes, and snippets.

@baoyongzhang
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baoyongzhang/b2b63705b7b301f3031a to your computer and use it in GitHub Desktop.
Save baoyongzhang/b2b63705b7b301f3031a to your computer and use it in GitHub Desktop.
Java线程池异常会被吞掉,需要手动获取异常信息
mExecutor = new ScheduledThreadPoolExecutor(6) {
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
Future<?> future = (Future<?>) r;
if (future.isDone())
future.get();
} catch (CancellationException ce) {
t = ce;
} catch (ExecutionException ee) {
t = ee.getCause();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // ignore/reset
}
}
if (t != null)
t.printStackTrace();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment