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)
  • 相关阅读:
    Stream流之三级查询
    SpringBoot日期格式的设置
    el表达式
    SpringMV+HuTool之验证码登录
    Spring注解详解
    @ResponseBody注解使用(返回字符串并不跳转)
    每日leetcode-数组-589. N 叉树的前序遍历
    python apply函数
    剑指offer-JZ6 旋转数组的最小数字
    torch.manual_seed()函数
  • 原文地址:https://www.cnblogs.com/weilf/p/5210869.html
Copyright © 2011-2022 走看看