AsyncTask的不足(待续)
来源:互联网 发布:网络教育和函授的区别 编辑:程序博客网 时间:2024/05/01 03:06
下文来自:
http://foo.jasonhudgins.com/2010/05/limitations-of-asynctask.html
=============================================================
English 原文:
AsyncTask is a fine API, it's been said that it "holds your hand", and makes performing background operations painless. It pulls this off so well in fact, that I see people overusing it in situations where it's not really appropriate.
It's particularly unsuited for situations when you have a multiple tasks to perform concurrently. Imagine an Activity that needs to download about 30 small images from a remote server, and update the UI as these become available. AsyncTask uses a static internal work queue with a hard-coded limit of 10 elements. That means if you were to create an AsyncTask instance for each image, the work queue would quickly overflow and many of your tasks would get rejected. The best solution in this case is to create your own ThreadPoolExecutor instance that uses a queue that's large enough to hold all your tasks, if you need an unbounded queue, a LinkedBlockingQueue will work just fine.
Another severe limitation is that an AsyncTask can't survive your Activity being torn down and recreated on the other side. Even if you pass it to the new instance via onRetainNonConfigurationState, the internal Handler inside the AsyncTask is still going to be stale and it's not going to behave correctly. This is important to consider, and the Android documentation makes no mention of it all. I've already blogged this scenario in detail, so I won't flog a dead horse here.
There are a few other minor issues, such as the fact thatyou can't change the background threads execution priority. It's hard-coded to a low priority, which granted, is the sensible default. Also, exception handling is not very well supported.
So just to reiterate, AsyncTask is a nice API, but you should understand it's limitations and apply it appropriately. If you're serious about writing mobile apps, then you're going to need a few more tools in the toolbox to get the job done.
中文翻译:
- AsyncTask的不足(待续)
- Android项目-高考作文-AsyncTask的不足
- AsyncTask原理及不足
- AsyncTask(一)AsyncTask的使用
- 找工作的经历(待续)
- 精巧的代码(待续)
- 自己的缺点(待续)
- Ortp的使用(待续)
- excel的快捷键(待续)
- nmap的使用(待续)
- SqlCommandBuilder的不足与进化(一)
- AsyncTask的用法(转)
- AsyncTask的理解(一)
- JSF的权限设计(待续)。
- db2常用的管理命令(待续)
- 一个程序员的出路(待续)
- CString 的装换(待续)
- 员工手册的内容(待续)
- JavaScript与C# Windows应用程序交互(webBrowser方法)
- 忘记sa密码
- Myeclipse下informix中文乱码解决方法
- Linux环境下使用Libxml2库
- Android 获取当前系统语言及地区,并更改语言
- AsyncTask的不足(待续)
- 忘记Oracle sys 密码
- aspnetpager 分页存储过程的实现
- Linux服务器上11种网络连接状态 和 TCP三次握手/四次挥手详解
- java基础---->可变参数
- text的取值,设置属性
- Oracle常见问题
- 用不相交集数据结构生成迷宫
- jsonP服务端代码