请问 AsyncTask 中的 cancel(true) 和 cancel(false) 的区别体现在哪里? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
XhstormR

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

  •  
  •   XhstormR Oct 2, 2016 14259 views
    This topic created in 3493 days ago, the information mentioned may be changed or developed.

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

    • 都不会调用 onPostExecute ,而会调用 onCancelled 。
    • 在 doInBackground 方法中调用 isCancel 都会返回 true 。

    那么区别体现在哪里呢?

    12 replies    2016-10-03 18:03:04 +08:00
    bqbkbz
        1
    bqbkbz  
       Oct 2, 2016
    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
        2
    XhstormR  
    OP
       Oct 2, 2016
    @zhaohui318
    这个文档我看了,他的意思是在 doInBackground 中通过 isCancel 来判断我传入的 mayInterruptIfRunning 值。

    但是 cancel(true) 和 cancel(false) 的 isCancel 都返回 true ,我找不出他们的区别。
    iyeatse
        3
    iyeatse  
       Oct 2, 2016
    mayInterruptIfRunning 为 true 时会调用 Thread.interrupt() 方法
    iyeatse
        4
    iyeatse  
       Oct 2, 2016   1
    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
        5
    kaedea  
       Oct 2, 2016
    突然也想不清楚了,看了一下笔记:

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

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

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

    我的例子是在 doInBackground 方法中循环 10 次 +1 ,都正常执行完毕了。
    kaedea
        11
    kaedea  
       Oct 3, 2016
    @XhstormR 你需要在循环体内做 isCancelled 判断
    AsyncTask 说白了就是 Executor + Handler
    XhstormR
        12
    XhstormR  
    OP
       Oct 3, 2016
    @kaedea 嗯,这个我知道,就是纠结 cancel(false) 和 cancel(true)。

    算了,本来就没多大问题,就不纠结了,就都写 cancel(true)。
    About     Help     Advertise     Blog     API     FAQ     Solana     1173 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 23:48 PVG 07:48 LAX 16:48 JFK 19:48
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86