方法cancel()和方法interrupt()是否做重复的工作?

发布于 2024-12-04 14:40:18 字数 808 浏览 7 评论 0原文

我阅读了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

死开点丶别碍眼 2024-12-11 14:40:18

它们通常不会做同样的事情,因为它们作用于不同的抽象级别(任务是比线程更高的抽象级别)。然而,在这种情况下,这些调用似乎是多余的。

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 (if true is passed as the argument) will attempt to interrupt the Thread on which the task is currently running (if any).

t.interrupt() attempts to interrupt the Thread t.

In this case it seems to be redundant. If the Task is still running, then cancel(true) should interrupt the thread, in which case the duplicate interrupt() 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() and interrupt() will have no effet.

情释 2024-12-11 14:40:18

在此,我想得出一个结论:
当我们将 true 作为 FutureTask.cancel() 的参数传递时,我们可以获得与 interupt() 相同的效果。
为什么?
让我们看一下 cancel() 方法的 src。
我们知道cancel()方法调用了该方法:

innerCancel(mayInterruptIfRunning);

当在方法内部时:innerCancel(mayInterruptIfRunning);,我们可以得到以下说明:

if (mayInterruptIfRunning) {
                Thread r = runner;
                if (r != null)
                    r.interrupt();
            }

所以,在我的例子中,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:

innerCancel(mayInterruptIfRunning);

when inside the method:innerCancel(mayInterruptIfRunning);,we can have the instructions below:

if (mayInterruptIfRunning) {
                Thread r = runner;
                if (r != null)
                    r.interrupt();
            }

So,in my case,the cancel() actually call the interrupt() indeed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文