方法cancel()和方法interrupt()是否做重复的工作?
我阅读了 org.apache.nutch.parse.ParseUtil.runParser(Parser p, Content content) 的源代码。
这两个方法调用做同样的事情吗:
指令 1:
t.interrupt();
指令 2:
task.cancel(true);
org.apache.nutch.parse.ParseUtil.runParser(Parser p, Content content) 的来源是:
ParseCallable pc = new ParseCallable(p, content);
FutureTask<ParseResult> task = new FutureTask<ParseResult>(pc);
ParseResult res = null;
Thread t = new Thread(task);
t.start();
try {
res = task.get(MAX_PARSE_TIME, TimeUnit.SECONDS);
} catch (TimeoutException e) {
LOG.warn("TIMEOUT parsing " + content.getUrl() + " with " + p);
} catch (Exception e) {
task.cancel(true);
res = null;
t.interrupt();
} finally {
t = null;
pc = null;
}
return res;
I read the source of org.apache.nutch.parse.ParseUtil.runParser(Parser p, Content content)
.
Do these two method calls do the same thing:
Instruction 1:
t.interrupt();
Instruction 2:
task.cancel(true);
The source of the org.apache.nutch.parse.ParseUtil.runParser(Parser p, Content content)
is:
ParseCallable pc = new ParseCallable(p, content);
FutureTask<ParseResult> task = new FutureTask<ParseResult>(pc);
ParseResult res = null;
Thread t = new Thread(task);
t.start();
try {
res = task.get(MAX_PARSE_TIME, TimeUnit.SECONDS);
} catch (TimeoutException e) {
LOG.warn("TIMEOUT parsing " + content.getUrl() + " with " + p);
} catch (Exception e) {
task.cancel(true);
res = null;
t.interrupt();
} finally {
t = null;
pc = null;
}
return res;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们通常不会做同样的事情,因为它们作用于不同的抽象级别(任务是比线程更高的抽象级别)。然而,在这种情况下,这些调用似乎是多余的。
FutureTask。 cancel()
告诉任务它不再需要运行,并且(如果true
作为参数传递)将尝试中断Thread
任务所在当前正在运行(如果有)。t.interrupt( )
尝试中断Thread
t
。在这种情况下,它似乎是多余的。 如果
Task
仍在运行,则cancel(true)
应该中断线程,在这种情况下重复interrupt()
调用是不必要的(除非线程中运行的代码以某种方式忽略一个中断,但在两个中断时停止,这是不可能的) 。如果此时任务已完成,则
cancel()
和interrupt()
都不会产生任何影响。They don't usually do the same thing, as they act on different abstraction levels (tasks being a higher abstraction levels than threads). In this case, however the calls seem to be redundant.
FutureTask.cancel()
tells the task that it no longer needs to run and (iftrue
is passed as the argument) will attempt to interrupt theThread
on which the task is currently running (if any).t.interrupt()
attempts to interrupt theThread
t
.In this case it seems to be redundant. If the
Task
is still running, thencancel(true)
should interrupt the thread, in which case the duplicateinterrupt()
call is unnecessary (unless the code running in the thread somehow ignores one interruption, but halts on two interrupts, which is unlikely).If the Task is already complete at that point, then both
cancel()
andinterrupt()
will have no effet.在此,我想得出一个结论:
当我们将 true 作为 FutureTask.cancel() 的参数传递时,我们可以获得与 interupt() 相同的效果。
为什么?
让我们看一下 cancel() 方法的 src。
我们知道cancel()方法调用了该方法:
当在方法内部时:
innerCancel(mayInterruptIfRunning);
,我们可以得到以下说明:所以,在我的例子中,cancel()实际上调用了确实是interrupt()。
Here,I'd like to make up a conclusion:
when we pass the true as the argument of the FutureTask.cancel(),we can get the same effect as the interupt() does yet.
why?
Let's peek into the src of cancel() method.
we got that the cancel() method call the method:
when inside the method:
innerCancel(mayInterruptIfRunning);
,we can have the instructions below:So,in my case,the cancel() actually call the interrupt() indeed.