jdk 在什么地方处理Callable的call方法的Exception呢?

来源:互联网 发布:车床马扎克640数控编程 编辑:程序博客网 时间:2024/04/28 07:01
 

1 FutureTask$Sync里执行callable.call()方法
void innerRun() {
            if (!compareAndSetState(0, RUNNING))
                return;
            try {
                runner = Thread.currentThread();
                if (getState() == RUNNING) // recheck after setting thread
                    innerSet(callable.call());
                else
                    releaseShared(0); // cancel
            } catch (Throwable ex) {
                innerSetException(ex);
            }
        }
       
2 然后赋值给 exception = t;
 void innerSetException(Throwable t) {
     for (;;) {
  int s = getState();
  if (s == RAN)
      return;
                if (s == CANCELLED) {
      // aggressively release to set runner to null,
      // in case we are racing with a cancel request
      // that will try to interrupt runner
                    releaseShared(0);
                    return;
                }
  if (compareAndSetState(s, RAN)) {
                    exception = t;
                    result = null;
                    releaseShared(0);
                    done();
      return;
                }
     }
        }
       
3 然后在get的时候:
        V innerGet() throws InterruptedException, ExecutionException {
            acquireSharedInterruptibly(0);
            if (getState() == CANCELLED)
                throw new CancellationException();
            if (exception != null)
                throw new ExecutionException(exception);
            return result;
        }

        V innerGet(long nanosTimeout) throws InterruptedException, ExecutionException, TimeoutException {
            if (!tryAcquireSharedNanos(0, nanosTimeout))
                throw new TimeoutException();
            if (getState() == CANCELLED)
                throw new CancellationException();
            if (exception != null)
                throw new ExecutionException(exception);
            return result;
        }