public abstract class AsyncTask<Params, Progress, Result> // Params Progress Result 分别时三个范性,代指参数、任务执行进度、返回结果体
// API 29 与 API 28略有不同 private static final int CORE_POOL_SIZE = 1; // 核心线程数 private static final int MAXIMUM_POOL_SIZE = 20; // 最大线程数 private static final int BACKUP_POOL_SIZE = 5; // 备份线程数 private static final int KEEP_ALIVE_SECONDS = 3; // 空闲线程空闲存活时间 // 线程工厂 private static final ThreadFactory sThreadFactory = new ThreadFactory() { // 线程数原子类 保证原子操作 private final AtomicInteger mCount = new AtomicInteger(1); // 重写该方法是为了对线程加 别名 AsyncTask # public Thread newThread(Runnable r) { return new Thread(r, "AsyncTask #" + mCount.getAndIncrement()); } }; // 当任务超出时的拒绝策略。 private static final RejectedExecutionHandler sRunOnSerialPolicy = new RejectedExecutionHandler() { public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { android.util.Log.w(LOG_TAG, "Exceeded ThreadPoolExecutor pool size"); // As a last ditch fallback, run it on an executor with an unbounded queue. // Create this executor lazily, hopefully almost never. synchronized (this) { if (sBackupExecutor == null) { // 当任务添加到队列失败,并且最大线程数已满,此时会新建一个备份线程池去执行任务 sBackupExecutorQueue = new LinkedBlockingQueue<Runnable>(); sBackupExecutor = new ThreadPoolExecutor( BACKUP_POOL_SIZE, BACKUP_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS, sBackupExecutorQueue, sThreadFactory); sBackupExecutor.allowCoreThreadTimeOut(true); } } sBackupExecutor.execute(r); } };
public static final Executor THREAD_POOL_EXECUTOR; // 类加载时就会执行 初始化线程池 注意初入的队列参数为 new SynchronousQueue<Runnable>() 任务添加都会失败,都会开启新线程执行直到最大线程数满 static { ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor( CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), sThreadFactory); threadPoolExecutor.setRejectedExecutionHandler(sRunOnSerialPolicy); THREAD_POOL_EXECUTOR = threadPoolExecutor; }
直接查看 execute 执行任务函数
execute
execute函数存在重载
1 2 3 4 5 6 7 8 9
@MainThread public final AsyncTask<Params, Progress, Result> execute(Params... params) { return executeOnExecutor(sDefaultExecutor, params); }
@MainThread public static void execute(Runnable runnable) { sDefaultExecutor.execute(runnable); }
// 此处传入的 exec 也是 一个 SerialExecutor 对象。 @MainThread 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;
private Handler getHandler() { return mHandler; } private static class InternalHandler extends Handler { public InternalHandler(Looper looper) { super(looper); }
@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; } } }