为什么没有“awaitTermination(日期截止日期)”方法?

发布于 2024-11-30 21:58:14 字数 529 浏览 1 评论 0原文

我有一个提交给 ExecutorService 的任务列表。但我需要在凌晨 2:30 的截止日期之前关闭 ExecutorService,即使任务尚未完成。我怎样才能实现这个目标?我查了一下API,只有一个像下面这样的方法:

ExecutorService exec = //...    
exec.awaitTermination(100, TimeUnit.MILLISECONDS);

但是我怎样才能让下面的块原子地执行呢?也就是说,怎样才能避免这个差距呢?例如:

long timeDiff= calculate(now, deadline);
// Gap: assuming current thread does not have chance to run for 10 minutes...
exec.awaitTermination(timeDiff, TimeUnit.MILLISECONDS);

谢谢。

I have a list of tasks submitted to an ExecutorService. But I need to shutdown the ExecutorService before a deadline of 2:30AM, even if the tasks are not finished. How can I achieve this? I checked the API, there is only a method like below:

ExecutorService exec = //...    
exec.awaitTermination(100, TimeUnit.MILLISECONDS);

But how can I make the following block executing atomically? That is, how can I avoid the gap? For example:

long timeDiff= calculate(now, deadline);
// Gap: assuming current thread does not have chance to run for 10 minutes...
exec.awaitTermination(timeDiff, TimeUnit.MILLISECONDS);

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

月牙弯弯 2024-12-07 21:58:14

您可能不是指“原子地”,我认为您的意思是“立即”。因此,在调用 exec.awaitTermination() 时,timeDiff 仍然是正确的。

我认为这是正确的,所以答案是:你不能

如果您对详细信息感兴趣:
您的 Java 代码被转换为 Java 字节码,并且这些代码由 JVM 执行,JVM 是操作系统上运行的常规进程。而且您根本无法阻止操作系统中断线程(我假设您使用具有抢占式多任务处理功能的操作系统(每个 UNIX(包括 Linux 和 Mac OS X)、Windows 95 或更高版本,...))。

即使您可以在一个 Java 字节码中完成所有这些操作,它仍然无法按您希望的方式工作,因为操作系统可能会在一个 Java 字节码中间中断您。

即使是 awaitTermination(Date Deadline) 方法在这里也无济于事。它也必须由某人来实施。

你能做的最好的事情就是用尽可能少的字节码来完成它。

如果我是你,我可能会像你的代码那样做。

然而,这个可能更精确一些:

Date deadline = ....;

final TimerTask stopTask = new TimerTask() {
    public void run() {
        exec.shutdownNow();
    }
};

new Timer().schedule(stopTask, deadline);

但正如我所说:没有真正保证shotdownNow()截止日期立即执行。实际上,将deadline设置为实际截止日期前一秒应该没问题:-)

You probably don't mean 'atomically', I think you mean 'without delay' here. So that timeDiff is still correct when calling exec.awaitTermination().

I assume that's correct, so the answer is: you can't.

If you're interested in the details:
Your Java Code gets translated to Java Bytecodes and these get executed by the JVM which is a regular process running on your operating system. And you simply can't stop the operating system from interrupting threads (I assume you use a operating system with preemptive multitasking (every UNIX (including Linux and Mac OS X), Windows 95 or better, ...)).

Even if you could do all that in one Java Bytecode it would still not work as you want it to because the operating system could interrupt you in the middle of one Java Bytecode.

And even a awaitTermination(Date deadline) method wouldn't help here. It has to be implemented by someone, too.

The best you can do is to do it in as few bytecodes as possible.

If I were you, I'd probably do just as your code does it.

However, that could be a bit more precise:

Date deadline = ....;

final TimerTask stopTask = new TimerTask() {
    public void run() {
        exec.shutdownNow();
    }
};

new Timer().schedule(stopTask, deadline);

But as I said: There is no real guarantee shotdownNow() gets executed IMMEDIATELY at deadline. In reality, setting deadline to one second before the real deadline should be okay :-)

唔猫 2024-12-07 21:58:14

在另一个高优先级线程(或计时器)中等待截止时间并调用 exec.shutdownNow()

Wait deadline in another high priority thread (or timer) and call exec.shutdownNow()

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