InterruptedException在Guava中使用Throwables.propagate(e)时,处理s 的最佳实践是什么?
我喜欢使用throw Throwables.propagate(e),特别是在不抛出任何检查异常的方法中,以及异常处理是调用者的责任.但它没有做我对InterruptedException的期望.
我不想失去线程被中断的事实,所以我最终写下这样的事情:
public void run() {
Callable c = ...;
try {
c.call();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw Throwables.propagate(e);
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在番石榴做到这一点?是否存在(向后兼容的?!)方式使用类似Throwables.propagate()的方法将线程设置为中断,如果它正在包装并传播InterruptedException?
在番石榴中,是否有方便将a转换Function<T, Boolean>为Predicate<T>?
在Functions,我可以看到,Function<T, Boolean> forPredicate(Predicate<T> predicate)但我没有看到任何朝向另一个方向的东西.
我现在要编写的代码是:
public static <T> Predicate<T> functionToPredicate(final Function<T, Boolean> func) {
return new Predicate<T>() {
@Override
public boolean apply(T input) {
return func.apply(input);
}
};
}
Run Code Online (Sandbox Code Playgroud)