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) {</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>
}
1
monetto OP 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; } } |
2
monetto OP 传入参数是 false 了,但是还是终止执行了。get 方法会报异常了。这是怎么回事??
求大神帮忙看看 |
3
chendy 2019-08-30 17:33:43 +08:00
复制粘贴运行
123 true 33 无异常 |
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 ExecutionException((Throwable)x); } ----------- 这里 result.cancel(false); 将线程终止,修改任务状态 state,后来执行 result.get()根据 state 状态已关闭抛出异常 |