[英]Android AsyncTask vs Thread + Handler vs rxjava
I know this is the question which was asked many many times. 我知道这是多次被问到的问题。 However there is something I never found an answer for.
然而,有些东西我从来没有找到答案。 So hopefully someone can shed me some light.
所以希望有人能给我一些启示。
We all know that AsyncTask and Thread are options for executing background tasks to avoid ANR issue. 我们都知道AsyncTask和Thread是执行后台任务以避免ANR问题的选项。 It is recommended that asynctask should only be used for short-running tasks while thread can be used for long-running tasks.
建议asynctask只应用于短期运行任务,而线程可用于长时间运行的任务。 The reasons why asynctask shouldn't be used for long tasks are well-known which is about the possible leak caused by asynctask since it may continue running after an activity's destroyed.
asynctask不应该用于长任务的原因众所周知,这是因为asynctask可能导致泄漏,因为它可能会在活动被破坏后继续运行。 That is convincing.
这是令人信服的。 However, it also leads to some other questions:
但是,它还会导致一些其他问题:
Thanks 谢谢
Since no one's replying. 既然没有人回复。 I'm answering my own questions then.
我正在回答我自己的问题。
AsyncTask
is recommended for only short tasks (around 5 seconds) is there is no method to cancel a running AsyncTask
. AsyncTask
建议仅用于短任务(大约5秒)的原因是没有方法可以取消正在运行的AsyncTask
。 There exists a method called AsyncTask.cancel(true)
which invokes onCancelled(Result result)
. AsyncTask.cancel(true)
的方法,它调用onCancelled(Result result)
。 However, according to the docs, this method "runs on the UI thread after cancel(boolean) is invoked and doInBackground(Object[]) has finished." Thread
can be stopped with Thread.interrupt()
. Thread.interrupt()
停止Thread
。 AsyncTask
within a Service
provided that you are aware of the cancellation limitation of AsyncTask
and the possibility of memory leak can be created by AsyncTask
. AsyncTask
的取消限制并且可以通过AsyncTask
创建内存泄漏的可能性,那么在Service
运行AsyncTask
应该没有任何问题。 Note that, there is obviously no need to use an AsyncTask
in an IntentService
which is already running in a worker thread. IntentService
使用AsyncTask
。 RxJava
all the time without having any issue. RxJava
而没有任何问题。 Note that the same memory leaking issue is also applied to RxJava
. RxJava
。 You can perhaps find one of the specific questions here . RxJava
that can be easily found by Googling. RxJava
处理泄漏/屏幕旋转的讨论还有很多,Googling可以很容易地找到它们。 AsyncTask and Thread+Handler are not carefully designed and implemented. AsyncTask和Thread + Handler没有经过精心设计和实现。 RxJava, Akka and other frameworks for asynchronous execution seem more carefully developed.
RxJava,Akka和其他异步执行框架似乎更加精心开发。
Each technology has its limitations. 每种技术都有其局限性。 AsyncTask is for a single parallel task with ability to show progress on UI.
AsyncTask用于单个并行任务,能够在UI上显示进度。 However, if activity is regenerated (eg because of screen rotating), connection to UI is lost (one possible solution for this problem is at https://github.com/rfqu/AsyncConnector ).
但是,如果重新生成活动(例如,由于屏幕旋转),则会丢失与UI的连接(此问题的一种可能解决方案是https://github.com/rfqu/AsyncConnector )。
Thread+Handler
keeps memory for thread stack even when there is no messages to process. 即使没有要处理的消息,
Thread+Handler
也会为线程堆栈保留内存。 This limits the possible number of threads. 这限制了可能的线程数。 You can have much more
Akka actors
or RxJava Subscribers
than handler threads, with similar functionality. 您可以拥有比处理程序线程更多的
Akka actors
或RxJava Subscribers
,具有类似的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.