如何将 AsyncTask 与 ThreadPoolExecutor 一起使用
我对 ThreadPoolExecutor 有点困惑。这是我想要完成的任务:
我有一个列表视图,左侧填充有图像图标,右侧填充有简短说明。我已经静态定义了所有文本,但是我想从网络获取图标。我已经知道如何从网址获取图像,但是我不想为每个图标生成“n”个线程来获取图标图像。所以我读了有关 asynctask 的 threadpoolexecutor 的文章,但我不知道如何去做。你们能给我一个先机吗?我是否必须创建一个 threadpoolexecutor 并在其中使用 asynctasks ?
我的列表是这样的..没有图标。
___________________________
[icon][a short description]
____________________________
[icon][a short description]
_____________________________
[icon][a short description]
_____________________________
[icon][a short description]
____________________________
I am a little confused on ThreadPoolExecutor. Here is what I am trying to accomplish:
I have a list-view which is populated with an image icon to the left and a short description to its right. I have all of the text already defined statically, however i want to get the icons from the the web. I already know how to get the image from a url, however I dont want to spawn 'n' amount of threads per icon to grab the icon image. So i read up on threadpoolexecutor for asynctask and I am not sure how to go about it. can you guys give me a head start? do I have to create a threadpoolexecutor and use asynctasks within it?
My list is like this.. without the icons.
___________________________
[icon][a short description]
____________________________
[icon][a short description]
_____________________________
[icon][a short description]
_____________________________
[icon][a short description]
____________________________
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议只使用 AsyncTask,而不用担心 ThreadPoolExecutor,无论如何它都需要 HoneyComb。 AsyncTask 有一个线程池,将被所有列表项重用。它不会为每个图标创建一堆线程。
请记住,您的任务将在 Android 2.x 上并行运行,并跟踪您的代码可能存在的任何同步问题。
I would recommend just using AsyncTask and not worry about the
ThreadPoolExecutor
, which requires HoneyComb anyway. AsyncTask has a pool of threads that will be reused for all of your list items. It won't create a bunch of threads per icon.Just keep in mind that your tasks will be running in parallel on Android 2.x and to keep track of any synchronization issues your code might have.
您将需要使用方法 executeOnExecutor() 使用您自己的执行器启动它。
似乎有很多陷阱,因此请仔细阅读
AsyncTask
的整个页面。所以是的,您可以创建自己的 ThreadPoolExecutor 并将其与 AsyncTask 一起使用。
You will need to use method executeOnExecutor() to start it with your own executor.
There seems to be number of gotchas, so read carefully the entire page for
AsyncTask
.So yes, you can create your own
ThreadPoolExecutor
and use it withAsyncTask
.