Skip to content

Commit

Permalink
springside#539 捕捉线程运行时的异常,防止跑飞的Runnable改名为SafeRunnable
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Jan 21, 2017
1 parent b78c589 commit 72e1e8f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* 2. 创建可自定义线程名的ThreadFactory(via guava)
*
* 3. 防止第三方Runnalbe未捕捉异常导致线程跑飞
* 3. 防止第三方Runnable未捕捉异常导致线程跑飞
*
* @author calvin
*
Expand Down Expand Up @@ -81,24 +81,24 @@ public static ThreadFactory buildThreadFactory(@NotNull String threadNamePrefix,
}

/**
* 防止用户没有捕捉异常导致中断了线程池中的线程, 使得SchedulerService无法执行.
* 防止用户没有捕捉异常导致中断了线程池中的线程, 使得SchedulerService无法继续执行.
*
* 在无法控制第三方包的Runnalbe实现时,调用本函数进行包裹.
* 在无法控制第三方包的Runnable实现时,调用本函数进行包裹.
*/
public static Runnable wrapException(@NotNull Runnable runnable) {
return new WrapExceptionRunnable(runnable);
public static Runnable safeRunnable(@NotNull Runnable runnable) {
return new SafeRunnable(runnable);
}

/**
* 保证不会有Exception抛出到线程池的Runnable包裹类,防止用户没有捕捉异常导致中断了线程池中的线程, 使得SchedulerService无法执行. 在无法控制第三方包的Runnalbe实现时,使用本类进行包裹.
*/
public static class WrapExceptionRunnable implements Runnable {
public static class SafeRunnable implements Runnable {

private static Logger logger = LoggerFactory.getLogger(WrapExceptionRunnable.class);
private static Logger logger = LoggerFactory.getLogger(SafeRunnable.class);

private Runnable runnable;

public WrapExceptionRunnable(Runnable runnable) {
public SafeRunnable(Runnable runnable) {
Validate.notNull(runnable);
this.runnable = runnable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void wrapException() {
////////
executor = ThreadPoolBuilder.scheduledPool().build();
ExceptionTask newTask = new ExceptionTask();
Runnable wrapTask = ThreadPoolUtil.wrapException(newTask);
Runnable wrapTask = ThreadPoolUtil.safeRunnable(newTask);
executor.scheduleAtFixedRate(wrapTask, 0, 100, TimeUnit.MILLISECONDS);

ThreadUtil.sleep(500);
Expand Down

0 comments on commit 72e1e8f

Please sign in to comment.