国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android AsyncTask實(shí)現(xiàn)機(jī)制詳細(xì)介紹及實(shí)例代碼

2019-12-12 04:15:58
字體:
供稿:網(wǎng)友

Android AsyncTask實(shí)現(xiàn)機(jī)制

示例代碼:

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;  }

execute先調(diào)用onPreExecute()(可見,onPreExecute是自動調(diào)用的)然后調(diào)用exec.execute(mFuture)

 public interface Executor {    void execute(Runnable command);  }

這是一個接口,具體實(shí)現(xiàn)在

 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);      }    }  }

從上面可知,AsyncTask執(zhí)行過程如下:先執(zhí)行onPreExecute,然后交給SerialExecutor執(zhí)行。在SerialExecutor中,先把Runnable添加到mTasks中。

如果沒有Runnable正在執(zhí)行,那么就調(diào)用SerialExecutor的scheduleNext。同時當(dāng)一個Runnable執(zhí)行完以后,繼續(xù)執(zhí)行下一個任務(wù)

AsyncTask中有兩個線程池,THREAD_POOL_EXECUTOR和SERIAL_EXECUTOR,以及一個Handler

主站蜘蛛池模板: 新乐市| 庆城县| 东乡县| 噶尔县| 临澧县| 镇康县| 北流市| 长沙县| 永和县| 尖扎县| 化德县| 长子县| 林州市| 通城县| 九龙县| 平山县| 马公市| 达州市| 甘谷县| 汕头市| 偏关县| 乃东县| 长治市| 进贤县| 昌吉市| 革吉县| 彰化县| 郧西县| 高要市| 荆门市| 蒲江县| 金川县| 尼木县| 高淳县| 宿松县| 辽宁省| 梅州市| 清涧县| 寻甸| 彝良县| 来安县|