FutureTask 的 cancel 方法为什么传参 false 依然会终止线程? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
monetto
V2EX    Java

FutureTask 的 cancel 方法为什么传参 false 依然会终止线程?

  •  
  •   monetto 2019-08-30 17:14:21 +08:00 3185 次点击
    这是一个创建于 2245 天前的主题,其中的信息可能已经有所发展或是发生改变。

    import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; import static java.lang.Thread.sleep;

    public class MainClass { public static void mai(String[] args) throws InterruptedException { FutureTask<integer> result = new FutureTask<integer>(new CallableThread()); Thread thread = new Thread(result); thread.start(); sleep(500); try { result.cancel(false); System.out.println(result.isCancelled()); sleep(2000); System.out.println("Get: " + result.get()); } catch (Exception e) {</integer></integer>

     } } 

    }

    class CallableThread implements Callable<integer> { public Integer call() throws Exception { System.out.println(123); sleep(2000); System.out.println(33); return 444; }</integer>

    }

    4 条回复    2019-08-30 17:58:07 +08:00
    monetto
        1
    monetto  
    OP
       2019-08-30 17:14:32 +08:00
    import java.util.concurrent.Callable;
    import java.util.concurrent.FutureTask;
    import static java.lang.Thread.sleep;

    public class MainClass {
    public static void main(String[] args) throws InterruptedException {
    FutureTask<Integer> result = new FutureTask<Integer>(new CallableThread());
    Thread thread = new Thread(result);
    thread.start();
    sleep(500);
    try {
    result.cancel(false);
    System.out.println(result.isCancelled());
    sleep(2000);
    System.out.println("Get: " + result.get());
    } catch (Exception e) {

    }
    }

    }

    class CallableThread implements Callable<Integer> {
    public Integer call() throws Exception {
    System.out.println(123);
    sleep(2000);
    System.out.println(33);
    return 444;
    }

    }
    monetto
        2
    monetto  
    OP
       2019-08-30 17:15:29 +08:00
    传入参数是 false 了,但是还是终止执行了。get 方法会报异常了。这是怎么回事??
    求大神帮忙看看
    chendy
        3
    chendy  
       2019-08-30 17:33:43 +08:00
    复制粘贴运行
    123
    true
    33
    无异常
    xzg
        4
    xzg  
       2019-08-30 17:58:07 +08:00
    复制黏贴
    123
    true
    33

    Process finished with exit code 0
    修改 catch 代码块抛出异常
    } catch (Exception e) {
    e.printStackTrace();
    }
    结果:
    java.util.concurrent.CancellationException
    at java.util.concurrent.FutureTask.report(FutureTask.java:121)
    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
    at cmdt.minibus.model.MainClass.main(MainClass.java:17)
    --------------------
    源码:
    设置 cancel,state 修改

    public boolean cancel(boolean mayInterruptIfRunning) {
    if (!(state == NEW &&
    UNSAFE.compareAndSwapInt(this, stateOffset, NEW,
    mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
    return false;
    try { // in case call to interrupt throws exception
    if (mayInterruptIfRunning) {
    try {
    Thread t = runner;
    if (t != null)
    t.interrupt();
    } finally { // final state
    UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);
    }
    }
    } finally {
    finishCompletion();
    }
    return true;
    }

    result.get 源码:
    public V get() throws InterruptedException, ExecutionException {
    int s = state;
    if (s <= COMPLETING)
    s = awaitDone(false, 0L);
    return report(s);
    }

    private V report(int s) throws ExecutionException {
    Object x = outcome;
    if (s == NORMAL)
    return (V)x;
    if (s >= CANCELLED)
    throw new CancellationException();
    throw new ExecutionExceptio((Throwable)x);
    }
    -----------
    这里 result.cancel(false); 将线程终止,修改任务状态 state,后来执行 result.get()根据 state 状态已关闭抛出异常
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2979 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 13:08 PVG 21:08 LAX 06:08 JFK 09:08
    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