zoukankan      html  css  js  c++  java
  • 谨慎使用AsyncTask

    AsyncTask估计很多人都使用过这个东西,感觉很美好的东西,asynchronize 特性,多线程支持但不需要关心具体线程执行细节,杠杠的好玩意儿。

    而事实上呢,稍有不慎,就是大悲剧。

    AsyncTask有段注释:

    * <p>AsyncTask is designed to be a helper class around {@link Thread} and {@link Handler}
    * and does not constitute a generic threading framework. AsyncTasks should ideally be
    * used for short operations (a few seconds at the most.) If you need to keep threads
    * running for long periods of time, it is highly recommended you use the various APIs
    * provided by the <code>java.util.concurrent</code> pacakge such as {@link Executor},
    * {@link ThreadPoolExecutor} and {@link FutureTask}.</p>

    貌似不起眼(或者说google是坑货不说明白),其实很关键。

    为何?参见这里或者看下面

    Indeed AsyncTask have two main issues that are related :

    • they are poorly tied to the activity life cycle
    • the create memory leaks very easily.

    Inside the RoboSpice Motivations app (available on Google Play) we answer that question in detail. It will give an in-depth view of AsyncTasks, Loaders, their features and drawbacks and also introduce you to an alternative solution for network requests : RoboSpice. Network requests are a common requirement in Android and are by nature long running operations . Here is an excerpt from the app :

    The AsyncTask and Activity life cycle

    AsyncTasks don't follow Activity instances' life cycle. If you start an AsyncTask inside an Activity and you rotate the device, the Activity will be destroyed and a new instance will be created. But the AsyncTask will not die. It will go on living until it completes.

    And when it completes, the AsyncTask won't update the UI of the new Activity. Indeed it updates the former instance of the activity that is not displayed anymore. This can lead to an Exception of the type java.lang.IllegalArgumentException: View not attached to window manager if you use, for instance, findViewById to retrieve a view inside the Activity.

    Memory leak issue

    It is very convenient to create AsyncTasks as inner classes of your Activities. As the AsyncTask will need to manipulate the views of the Activity when the task is complete or in progress, using an inner class of the Activity seems convenient : inner classes can access directly any field of the outer class.

    Nevertheless, it means the inner class will hold an invisible reference on its outer class instance : the Activity.

    On the long run, this produces a memory leak : if the AsyncTask lasts for long, it keeps the activity "alive" whereas Android would like to get rid of it as it can no longer be displayed. The activity can't be garbage collected and that's a central mechanism for Android to preserve resources on the device.


    It is really a very very bad idea to use AsyncTasks for long running operations. Nevertheless, they are fine for short living ones such as updating a View after 1 or 2 seconds.

    I encourage you to download the RoboSpice Motivations app, it really explains this in-depth and provides samples and demonstrations of the different ways to do some background operations.

  • 相关阅读:
    Bootstrap+Angularjs自制弹框
    【2019年04月22日】A股最便宜的股票
    沪深300指数的跟踪基金排名
    【2019年04月10日】股票的滚动市盈率PE最低排名
    【2019年04月09日】A股净资产收益率ROE最高排名
    基金 、社保和QFII等机构的重仓股排名评测
    【2019年04月04日】股市指数估值排名
    【2019年04月03日】A股最便宜的股票
    净资产收益率ROE连续3年超过15%的股票排名
    A股滚动净利润增速最高排名
  • 原文地址:https://www.cnblogs.com/chiefhsing/p/3299761.html
Copyright © 2011-2022 走看看