Android:AsyncTask或使用ExecutorService的普通Java线程 [英] Android: AsyncTask OR normal Java threads with ExecutorService

查看:123
本文介绍了Android:AsyncTask或使用ExecutorService的普通Java线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用服务来做一些背景知识。我在服务中使用其他线程来进行一些计算。为此,我每隔5到10秒创建两个线程,运行5到10秒。但我不知道我应该使用哪种线程模型:

My application uses a Service to do some background stuff. I am using additional threads in the service to do some computation. For this purpose I create two threads every 5 to 10 seconds, which are running 5 to 10 seconds. But I don't know which thread-model I should use:


  1. AsyncTask:

  1. AsyncTask:

优点:


  • 易于使用

  • 特定于Android的

  • 简单的用户体验互动

缺点:


  • 由于我必须使用API​​级别10,因此没有 ExecutorService 具有固定线程池来执行AsyncTasks

  • Since I have to use API level 10, there is no ExecutorService with fixed thread pool to execute the AsyncTasks

普通Java线程:

优点:


  • 具有固定线程池的ExecutorService

缺点:


  • 不太容易处理,例如UI交互

哪种型号更适合使用?特别是在关注性能方面。当我使用AsyncTasks时,是否存在繁重的开销,并且ExecutorService在重用线程方面比Android更快创建新的AsyncTasks?

Which model is better to use? Especially in concern of performance. Is there a heavy overhead when i am using AsyncTasks, and is the ExecutorService faster in reusing the threads than Android in creating new AsyncTasks?

推荐答案

如果你看一下 AsyncTask 的实现,你会看到它使用普通Java线程使用它自己的线程池。

If you look at the implementation of AsyncTask, you will see that it uses its own thread pool using "normal Java threads".


当我使用AsyncTasks时,是否有大量开销,并且ExecutorService在创建新的AsyncTasks时重用线程比Android更快?

Is there a heavy overhead when i am using AsyncTasks, and is the ExecutorService faster in reusing the threads than Android in creating new AsyncTasks?

两者之间应该没有实质性差异。

There should be no substantial difference between the two.

这篇关于Android:AsyncTask或使用ExecutorService的普通Java线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆