从android.os.AsyncTask借用源代码,并创建自己的com.company.AsyncTask实现,您可以在其中实现自己代码中所需的一切。
android.os.AsyncTask随附了两个现成的执行器THREAD_POOL_EXECUTOR和SERIAL_EXECUTOR:
private static final BlockingQueue<Runnable> sPoolWorkQueue = new linkedBlockingQueue<Runnable>(10);public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);public static final Executor SERIAL_EXECUTOR = new SerialExecutor();
在com.company.AsyncTask中,创建另一个PRIORITY_THREAD_POOL_EXECUTOR,并将您的所有实现包装在此类内(您对所有内部字段都具有忠诚度),然后像这样使用AysncTask:
com.company.AsyncTask asyncTask = new com.company.AsyncTask();asyncTask.setPriority(1);asyncTask.executeonExecutor(com.company.AsyncTask.PRIORITY_THREAD_POOL_EXECUTOR, (Void[]) null);
在这里查看我的答案,看看我如何创建自己的AsyncTask来使executeOnExecutor()在API级别11之前工作。



