AsyncTask是android sdk封装的异步操作类。 隐藏了Thread、 Runnable以及其他对象, 方便开发者使用。
1. AsyncTask 的使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 AsyncTask<Integer, Integer, Integer> asyncTask = new AsyncTask<Integer, Integer, Integer>() { @Override protected Integer doInBackground (Integer... params) { return null ; } @Override protected void onPreExecute () { super .onPreExecute(); } @Override protected void onPostExecute (Integer result) { super .onPostExecute(result); } @Override protected void onProgressUpdate (Integer... values) { super .onProgressUpdate(values); } @Override protected void onCancelled (Integer result) { super .onCancelled(result); } @Override protected void onCancelled () { super .onCancelled(); } }; } }
2. 任务执行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public final AsyncTask<Params, Progress, Result> execute (Params... params) { return executeOnExecutor(sDefaultExecutor, params); } public final AsyncTask<Params, Progress, Result> executeOnExecutor (Executor exec, Params... params) { if (mStatus != Status.PENDING) { switch (mStatus) { case RUNNING: throw new IllegalStateException("Cannot execute task:" + " the task is already running." ); case FINISHED: throw new IllegalStateException("Cannot execute task:" + " the task has already been executed " + "(a task can be executed only once)" ); } } mStatus = Status.RUNNING; onPreExecute(); mWorker.mParams = params; exec.execute(mFuture); return this ; }
执行任务,如果不配置Executor将使用默认的线程池。
3. 注意的地方,默认线程池在执行任务的时候串行执行(当一个任务执行完成后才执行下一个任务)。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 private static class SerialExecutor implements Executor { final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>(); Runnable mActive; public synchronized void execute (final Runnable r) { mTasks.offer(new Runnable() { public void run () { try { r.run(); } finally { scheduleNext(); } } }); if (mActive == null ) { scheduleNext(); } } protected synchronized void scheduleNext () { if ((mActive = mTasks.poll()) != null ) { THREAD_POOL_EXECUTOR.execute(mActive); } }
4 使用的是自定义runnable,为的是能够获取返回值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 mWorker = new WorkerRunnable<Params, Result>() { public Result call () throws Exception { mTaskInvoked.set(true ); Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); return postResult(doInBackground(mParams)); } }; mFuture = new FutureTask<Result>(mWorker) { @Override protected void done () { try { postResultIfNotInvoked(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) { postResultIfNotInvoked(null ); } } };
5 消息传递,最后执行完成还是通过Handler进行通信。 Handler源码解析
1 2 3 4 5 6 7 private Result postResult (Result result) { @SuppressWarnings ("unchecked" ) Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT, new AsyncTaskResult<Result>(this , result)); message.sendToTarget(); return result; }