WEB开发网
开发学院手机开发Android 开发 AsyncTask实现原理和内部代码 阅读

AsyncTask实现原理和内部代码

 2011-07-01 20:49:38 来源:本站整理   
核心提示:Android从1.5开始引入了AsyncTask这个类,可以帮助我们解决线程和界面刷新问题,AsyncTask实现原理和内部代码,主要是对Thread+Handler这样的封装,但在设计模式和代码维护方面都有不错的表现,内部处理Thread的状态, private final WorkerRunnable<P

Android从1.5开始引入了AsyncTask这个类,可以帮助我们解决线程和界面刷新问题,主要是对Thread+Handler这样的封装,但在设计模式和代码维护方面都有不错的表现。对于AsyncTask的实现原理和内部的代码如何实现Android123一起和大家分享,早在 2008年时Google推出了一个示例应用叫PhotoStream来演示UI在多线程网络慢速I/O下的刷新问题,里面的线程构造使用的正是 AsyncTask的雏形,由于内部使用Java 1.5的并发库比普通初级Android开发者编写的Thread+Handler稳定很多,下面我们就android.os.AsyncTask的实现

  public abstract class AsyncTask<Params, Progress, Result> {
   private static final String LOG_TAG = "AsyncTask";

   private static final int CORE_POOL_SIZE = 5; //线程池数量
   private static final int MAXIMUM_POOL_SIZE = 128; //线程池中最大线程数
   private static final int KEEP_ALIVE = 1;

   private static final BlockingQueue<Runnable> sWorkQueue =
       new LinkedBlockingQueue<Runnable>(10); //使用并发库的阻塞队列初始时保存10个Runnable对象

   private static final ThreadFactory sThreadFactory = new ThreadFactory() {
     private final AtomicInteger mCount = new AtomicInteger(1);

   public Thread newThread(Runnable r) {
       return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
     }
   };

   private static final ThreadPoolExecutor sExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,
       MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sWorkQueue, sThreadFactory); //创建线程池

   private static final int MESSAGE_POST_RESULT = 0x1;
   private static final int MESSAGE_POST_PROGRESS = 0x2;
   private static final int MESSAGE_POST_CANCEL = 0x3;

   private static final InternalHandler sHandler = new InternalHandler(); //这个是对Handler的封装,内部处理Thread的状态。

   private final WorkerRunnable<Params, Result> mWorker;  //该类对Runnable做简单封装
   private final FutureTask<Result> mFuture; //对于并发库而言FutureTask是最重要的,有兴趣的网友可以看下JDK源码

   private volatile Status mStatus = Status.PENDING; //保存当前线程状态

   public enum Status { //枚举类记录当前线程状态
     PENDING,
     RUNNING,
     FINISHED,
   }

   public AsyncTask() {
     mWorker = new WorkerRunnable<Params, Result>() { //构造Runable对象
       public Result call() throws Exception {
         Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); //设置线程优先级为后台,这里Android开发网提示大家低于标准线程优先级
         return doInBackground(mParams);
       }
     };

   mFuture = new FutureTask<Result>(mWorker) {
       @Override
       protected void done() {
         Message message;
         Result result = null;

       try {
           result = get();
         } catch (InterruptedException e) { //处理Thread中断异常
           android.util.Log.w(LOG_TAG, e);
         } catch (ExecutionException e) {
           throw new RuntimeException("An error occured while executing doInBackground()",

1 2 3  下一页

Tags:AsyncTask 实现 原理

编辑录入:coldstar [复制链接] [打 印]
赞助商链接