然后执行AsyncTask的doInBackground方法,并返回结果。所以当我们把耗时的工作量放在doInBackground方法里执行时,实际上是把大工作量放到了一个低级别的线程中去执行。
FutureTask中的run方法调用完callable.call()并获得结果之后就会调用set(result)方法,我们来看看set方法
- protected void set(V v) {
- sync.innerSet(v);
- }
- void innerSet(V v) {
- for (;;) {
- ...
- if (compareAndSetState(s, RAN)) {
- result = v;
- releaseShared(0);
- done();//调用了FutureTask的done();方法
- return;
- }
- }
- }
- mFuture = new FutureTask<Result>(mWorker) {
- @Override
- protected void done() {
- Message message;
- Result result = null;
- try {
- result = get();//调用FutureTask的get方法,该方法 如有必要,等待计算完成,然后获取其结果。
- } catch (InterruptedException e) {
- Android.util.Log.w(LOG_TAG, e);
- } catch (ExecutionException e) {
- throw new RuntimeException("An error occured while executing doInBackground()",
- e.getCause());
- } catch (CancellationException e) {
- message = sHandler.obtainMessage(MESSAGE_POST_CANCEL,
- new AsyncTaskResult<Result>(AsyncTask.this, (Result[]) null));//取消任务,由sHandler去处理
- message.sendToTarget();
- return;
- } catch (Throwable t) {
- throw new RuntimeException("An error occured while executing "
- + "doInBackground()", t);
- }
- message = sHandler.obtainMessage(MESSAGE_POST_RESULT,
- new AsyncTaskResult<Result>(AsyncTask.this, result));
- message.sendToTarget();
- }
- };
- public final boolean cancel(boolean mayInterruptIfRunning) {
- return mFuture.cancel(mayInterruptIfRunning);
- }
- ...
- } catch (CancellationException e) {
- message = sHandler.obtainMessage(MESSAGE_POST_CANCEL,
- new AsyncTaskResult<Result>(AsyncTask.this, (Result[]) null));//取消任务,由sHandler去处理
- message.sendToTarget();
- return;
- }
- ...
- ...
- message = sHandler.obtainMessage(MESSAGE_POST_RESULT,
- new AsyncTaskResult<Result>(AsyncTask.this, result));
- message.sendToTarget();
- ...
- private static class InternalHandler extends Handler {
- @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})
- @Override
- public void handleMessage(Message msg) {
- AsyncTaskResult result = (AsyncTaskResult) msg.obj;
- switch (msg.what) {
- case MESSAGE_POST_RESULT:
- // // There is only one result
- result.mTask.finish(result.mData[0]);
- break;
- case MESSAGE_POST_PROGRESS:
- result.mTask.onProgressUpdate(result.mData);
- break;
- case MESSAGE_POST_CANCEL:
- result.mTask.onCancelled();
- break;
- }
- }
- }
1、MESSAGE_POST_RESULT,成功执行完任务时,调用AsyncTask的finish(result)方法,参数为返回的结果
- AsyncTask的finish(result)方法:
- private void finish(Result result) {
- if (isCancelled()) result = null;
- onPostExecute(result);
- mStatus = Status.FINISHED;
- }
