zoukankan      html  css  js  c++  java
  • AsyncTask

    AsyncTask


    AsyncTask的设计目标是比较短的操作(最多几秒),如果希望执行更长时间,使用 java.util.concurrent 中的 Executor, ThreadPoolExecutor, FutureTask更合适。

    Asyntask的泛型实现:

    • Params:发送给Asyntask的执行参数类型
    • Progress:the type of the progress units published during the background computation
    • Result: background计算阶段的结果类型

    Asyntask执行中的四步:

    • onPreExecute() : 任务执行前执行,一般用途例如显示加载框,进度条
    • doInBackground(Params...) : 执行后台任务(途中可更新进度条)
    • onProgressUpdate(Progress...) : 更新进度条
    • onPostExecute(Result) :

    注意点

    • Asyntask实例必须在UI线程中简历
    • execute(Params...)必须在UI线程中调用。
      new DownloadFilesTask().execute(urlStr);
    • 不要手动调用onPreExecute(), onPostExecute()..等方法
    • The task can be executed only once(an exception will be thrown if a second execution is attempted)

    AsyncTask guarantees that all callback calls are synchronized in such a way that the following operations are safe without explicit synchronizations.

    • Set member fields in the constructor or onPreExecute(), and refer to them in doInBackground(Params...)
    • Set member fields in doInBackground(Params...), and refer to them in onProgressUpdate(Progress...) and onPostExecute(Result)
  • 相关阅读:
    springnodejs
    js CacheQueue
    权重练习
    架构师速成8.3-可用性之分库分表
    架构师速成8.3-可用性之分布式
    架构师速成8.3-可用性
    架构师速成8.3-架构师必须要了解的规则(转)
    架构师速成6.15-开发框架-单点登录
    架构师速成6.14-开发框架-异常处理
    架构师速成6.13-开发框架-前后结合
  • 原文地址:https://www.cnblogs.com/weilf/p/5210869.html
Copyright © 2011-2022 走看看