The limitations of AsyncTask
来源:互联网 发布:黑暗网络 编辑:程序博客网 时间:2024/06/01 10:10
The limitations of AsyncTask
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 viaonRetainNonConfigurationState, 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 that you 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.
- The limitations of AsyncTask
- Limitations of the Present Study
- Limitations of YOLO
- The Hidden Pitfalls of AsyncTask
- Limitations of Flashback Database【每日一译】--20121122
- Limitations of memory tracking features in Unreal Engine 4.
- 理解模拟器的限制(Understanding the Emulator's Limitations)
- Proper use of AsyncTask
- API of AsyncTask
- 知识库--Limitations of STM 【高并发写 not in favor】(137)
- MySQL Limitations Part 2: The Binary Log [MySQL 短板 2]
- Bypass Graphics.MeasureString limitations
- Windows Memory Allocation Limitations
- SMM Rootkit limitations.
- Django - Philosophies and Limitations
- General Rules and Limitations
- Webbrowser Control Limitations
- MSDN: Limitations on Raw Sockets
- 图文教你虚拟机安装Ubuntu
- CPL RPL DPL
- Windows RPC远程过程调用(1): 最基本的
- WinXP安装Ubuntu11.10
- Linux内核源代码分析5.28
- The limitations of AsyncTask
- PHP引用符&的用法介绍
- UNIX进程揭秘
- Linux2.6系统调用
- ajaxSingle
- 一个正确的宏
- 英语水平:第一级
- linux top命令详解
- 忘记 MySQL 的 root 帐号密码该怎么办