请问 AsyncTask 中的 cancel(true) 和 cancel(false) 的区别体现在哪里?

2016-10-02 16:44:08 +08:00
 XhstormR

我自己写了个测试,在执行 cancel(true) 和 cancel(false) 后:

那么区别体现在哪里呢?

7433 次点击
所在节点    Android
12 条回复
zhaohui318
2016-10-02 17:39:47 +08:00
https://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)

boolean cancel (boolean mayInterruptIfRunning)

mayInterruptIfRunning : true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete.
XhstormR
2016-10-02 17:55:01 +08:00
@zhaohui318
这个文档我看了,他的意思是在 doInBackground 中通过 isCancel 来判断我传入的 mayInterruptIfRunning 值。

但是 cancel(true) 和 cancel(false) 的 isCancel 都返回 true ,我找不出他们的区别。
iyeatse
2016-10-02 17:55:35 +08:00
mayInterruptIfRunning 为 true 时会调用 Thread.interrupt() 方法
iyeatse
2016-10-02 17:57:19 +08:00
http://blog.danlew.net/2014/06/21/the-hidden-pitfalls-of-asynctask/

> As for mayInterruptIfRunning – all it does is send an interrupt() to the running Thread. In the case that your Thread is uninterruptible, then it won ’ t stop the Thread at all.
kaedea
2016-10-02 18:30:24 +08:00
突然也想不清楚了,看了一下笔记:

AsyncTask 的 cancel 方法需要一个布尔值的参数,参数名为 mayInterruptIfRunning,意思是如果正在执行是否可以打断,如果这个值设置为 true ,表示这个任务可以被打断,否则,正在执行的程序会继续执行直到完成。如果在 doInBackground()方法中有一个循环操作,我们应该在循环中使用 isCancelled()来判断,如果返回为 true ,我们应该避免执行后续无用的循环操作。

也就是说,如果你没做好 isCancelled()判断的话,你的 doInBackground()就无法被取消了,哪怕你调用了 AysncTask.cancel(true)!这又是一个陷阱。
XhstormR
2016-10-02 20:08:46 +08:00
@kaedea
无论是 cancel(true),还是 cancel(false),调用之后 isCancelled() 都返回 true 。

所以 cancel 的这个 mayInterruptIfRunning 参数,说实话我是感觉没啥区别。
ilumer
2016-10-03 00:41:25 +08:00
弱弱的说一句会不会是你调用了 cancel 这个方法以后你的 iscancel 都会返回 true 。因为 cancel 方法中的 mCancelled.set(true);默认都是 set(ture)。同时 iscancelled 又是返回 mCancelled.get().都会返回相同的结果吧。
true 或者 false 的具体不同应该是影响的 cancel 方法的返回值以及线程的是否线程是否中断。
ddou
2016-10-03 08:04:04 +08:00
不做 android 开发,但是看文档应该是: true 的情况下,就算任务已经在执行,也会被打断,彻底取消任务。 false 时只能取消等待执行的任务,已经开始的任务不受影响。
kaedea
2016-10-03 16:40:14 +08:00
@XhstormR cancel(false)是无法打断已经开始运行的任务了
XhstormR
2016-10-03 17:08:38 +08:00
@kaedea 无论 true or false ,都无法中断正在执行中的 doInBackground 方法。

我的例子是在 doInBackground 方法中循环 10 次 +1 ,都正常执行完毕了。
kaedea
2016-10-03 17:26:36 +08:00
@XhstormR 你需要在循环体内做 isCancelled 判断
AsyncTask 说白了就是 Executor + Handler
XhstormR
2016-10-03 18:03:04 +08:00
@kaedea 嗯,这个我知道,就是纠结 cancel(false) 和 cancel(true)。

算了,本来就没多大问题,就不纠结了,就都写 cancel(true)。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/310287

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX