ExecutorService exec = Executors.newFixedThreadPool(8);
以上述代码为例,得到ExecutorService实例后,我们可以通过两种方式提交任务(Runnable):
exec.execute(runnable) 和 exec.submit(runnable)
exec.submit(runnable) 实际是调用AbstractExecutorService.submit(runnable),将Runnable包装为一个RunnableFuture对象,这个对象实际上是FutureTask实例,然后将这个FutureTask交给execute方法执行。
public abstract class AbstractExecutorService implements ExecutorService {
...
public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException();
RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask);
return ftask;
}
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
return new FutureTask<T>(runnable, value);
}
}
execute(ftask)会调用FutureTask的run()方法
public class FutureTask<V> implements RunnableFuture<V> {
...
public void run() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
public V get() throws InterruptedException, ExecutionException {
int s = state;
if (s <= COMPLETING)
s = awaitDone(false, 0L);
return report(s);
}
private V report(int s) throws ExecutionException {
Object x = outcome;
if (s == NORMAL)
return (V)x;
if (s >= CANCELLED)
throw new CancellationException();
throw new ExecutionException((Throwable)x);
}
}
FutureTask的run方法中,调用了callable对象的call方法,也就调用了我们传入的Runnable对象的run方法。可以看到,如果代码(Runnable)抛出异常,会被捕获并且通过setException(ex)方法把这个异常保存下来。
FutureTask的get方法中,会将保存的异常重新抛出。所以,在使用submit方法提交任务的时候,利用返回的Future对象的get方法可以得到任务运行中抛出的异常,然后针对异常做一些处理。
结论:使用ExecutorService.submit执行任务,利用返回的Future对象的get方法接收抛出的异常,然后进行处理