java cancel_Future.cancel()疑难杂症

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-31 07:36   70   0

问题描述

在学习并发编程的时候对Future的cancel有点小疑惑。

future类里面,有cancel()和isCanceled()的两个方法,前者是发送取消命令给线程并返回是否发送成功,后者是判断该线程是否被取消。但是在实际运用时出现了一些问题。以下会列出问题:

问题出现的环境背景及自己尝试过哪些方法

查阅了《java并发编程》,发现书里对isCanceled没有详细介绍

相关代码

问题

1.我对线程发送了cancel命令,但没有用isInterrupted来截获cancel命令,也就是说,线程还能一直执行下去。为什么isCanceled()返回了true呢?

public class Test10 {

class MyCallable implements Callable {

@Override

public String call() throws Exception {

int i = 0;

while (i == 0) {

System.out.println("z");

}

return "always run";

}

}

public static void main(String[] args) throws InterruptedException {

Test10 test10 = new Test10();

MyCallable myCallable = test10.new MyCallable();

ExecutorService executorService = new ThreadPoolExecutor(50, Integer.MAX_VALUE, 5, TimeUnit.SECONDS, new LinkedBlockingDeque());

Future future = executorService.submit(myCallable);

Thread.sleep(4000);

System.out.println(future.cancel(true) + " " + future.isCancelled());

}

}

这个是返回结果:

a1919ea069d50234b9d90943c707f9f6.png

2.当我对sleep的线程发出cancel命令,会发生什么呢?我试了下好像会一直处于sleep状态了

public class Test10 {

class MyCallable implements Callable {

@Override

public String call() throws Exception {

int i = 0;

while (i == 0) {

if (Thread.currentThread().isInterrupted()) {

System.out.println("截获中断");

}

Thread.sleep(1000);

System.out.println("z");

}

return "always run";

}

}

public static void main(String[] args) throws InterruptedException {

Test10 test10 = new Test10();

MyCallable myCallable = test10.new MyCallable();

ExecutorService executorService = new ThreadPoolExecutor(50, Integer.MAX_VALUE, 5, TimeUnit.SECONDS, new LinkedBlockingDeque());

Future future = executorService.submit(myCallable);

Thread.sleep(4000);

System.out.println(future.cancel(true) + " " + future.isCancelled());

}

}

这个是返回结果:

cc1db2e1922c4941f427be433ed5f947.png

你期待的结果是什么?实际看到的错误信息又是什么?

希望有大牛解惑,另外可以推荐一些并发编程的书吗?我看了《java并发编程:核心方法与框架》以及《java并发编程实战》了,总感觉学的比较浅。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP