网站开发证书,搜索引擎广告名词解释,巧克力软文范例200字,amp 网站开发分析#xff1a;在分析实现流程之前#xff0c;我们先了解一下AsyncTask有哪些成员变量。privatestaticfinalintCORE_POOL_SIZE 5;//5个核心工作线程privatestaticfinalintMAXIMUM_POOL_SIZE 128;//最多128个工作线程privatestaticfinalintKEEP_ALIVE 1;//空闲线程的超时时间…分析在分析实现流程之前我们先了解一下AsyncTask有哪些成员变量。privatestaticfinalintCORE_POOL_SIZE 5;//5个核心工作线程privatestaticfinalintMAXIMUM_POOL_SIZE 128;//最多128个工作线程privatestaticfinalintKEEP_ALIVE 1;//空闲线程的超时时间为1秒privatestaticfinalBlockingQueue sWorkQueue newLinkedBlockingQueue(10);//等待队列privatestaticfinalThreadPoolExecutorsExecutor newThreadPoolExecutor(CORE_POOL_SIZE,MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sWorkQueue,sThreadFactory);//线程池是静态变量所有的异步任务都会放到这个线程池的工作线程内执行。回到例子中点击按钮之后会新建一个GetCSDNLogoTask对象GetCSDNLogoTask task newGetCSDNLogoTask();此时会调用父类AsyncTask的构造函数AsyncTask.javapublicAsyncTask() {mWorker newWorkerRunnable() {publicResult call()throwsException {Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);returndoInBackground(mParams);}};mFuture newFutureTask(mWorker) {Overrideprotectedvoiddone() {Message message;Result result null;try{result get();}catch(InterruptedException e) {Android.util.Log.w(LOG_TAG, e);}catch(ExecutionException e) {thrownewRuntimeException(An error occured while executing doInBackground(),e.getCause());}catch(CancellationException e) {message sHandler.obtainMessage(MESSAGE_POST_CANCEL,newAsyncTaskResult(AsyncTask.this, (Result[])null));message.sendToTarget();//取消任务发送MESSAGE_POST_CANCEL消息return;}catch(Throwable t) {thrownewRuntimeException(An error occured while executing doInBackground(), t);}message sHandler.obtainMessage(MESSAGE_POST_RESULT,newAsyncTaskResult(AsyncTask.this, result));//完成任务发送MESSAGE_POST_RESULT消息并传递result对象message.sendToTarget();}};}WorkerRunnable类实现了callable接口的call()方法该函数会调用我们在AsyncTask子类中实现的doInBackground(mParams)方法由此可见WorkerRunnable封装了我们要执行的异步任务。FutureTask中的protected void done() {}方法实现了异步任务状态改变后的操作。当异步任务被取消会向UI线程传递MESSAGE_POST_CANCEL消息当任务成功执行会向UI线程传递MESSAGE_POST_RESULT消息并把执行结果传递到UI线程。由此可知AsyncTask在构造的时候已经定义好要异步执行的方法doInBackground(mParams)和任务状态变化后的操作(包括失败和成功)。当创建完GetCSDNLogoTask对象后执行task.execute(http://www.linuxidc.com/pic/logo.gif);此时会调用AsyncTask的execute(Params...params)方法AsyncTask.javapublicfinalAsyncTask execute(Params... params) {if(mStatus ! Status.PENDING) {switch(mStatus) {caseRUNNING:thrownewIllegalStateException(Cannot execute task: the taskis already running.);caseFINISHED:thrownewIllegalStateException(Cannot execute task: the taskhas already been executed (a task canbe executed only once));}}mStatus Status.RUNNING;onPreExecute();//运行在ui线程在提交任务到线程池之前执行mWorker.mParams params;sExecutor.execute(mFuture);//提交任务到线程池returnthis;}当任务正在执行或者已经完成会抛出IllegalStateException由此可知我们不能够重复调用execute(Params...params)方法。在提交任务到线程池之前调用了onPreExecute()方法。然后才执行sExecutor.execute(mFuture)是任务提交到线程池。前面我们说到当任务的状态发生改变时(1、执行成功2、取消执行3、进度更新)工作线程会向UI线程的Handler传递消息。在《Android异步处理三HandlerLooperMessageQueue深入详解》一文中我们提到Handler要处理其他线程传递过来的消息。在AsyncTask中InternalHandler是在UI线程上创建的它接收来自工作线程的消息实现代码如下AsyncTask.javaprivatestaticclassInternalHandlerextendsHandler {SuppressWarnings({unchecked,RawUseOfParameterizedType})OverridepublicvoidhandleMessage(Message msg) {AsyncTaskResult result (AsyncTaskResult) msg.obj;switch(msg.what) {caseMESSAGE_POST_RESULT:// There is onlyone resultresult.mTask.finish(result.mData[0]);//执行任务成功break;caseMESSAGE_POST_PROGRESS:result.mTask.onProgressUpdate(result.mData);//进度更新break;caseMESSAGE_POST_CANCEL:result.mTask.onCancelled();//取消任务break;}}}当接收到消息之后AsyncTask会调用自身相应的回调方法。总结1、 AsyncTask的本质是一个静态的线程池AsyncTask派生出的子类可以实现不同的异步任务这些任务都是提交到静态的线程池中执行。2、线程池中的工作线程执行doInBackground(mParams)方法执行异步任务3、当任务状态改变之后工作线程会向UI线程发送消息AsyncTask内部的InternalHandler响应这些消息并调用相关的回调函数