为什么没有“awaitTermination(日期截止日期)”方法?
我有一个提交给 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能不是指“原子地”,我认为您的意思是“立即”。因此,在调用
exec.awaitTermination()
时,timeDiff
仍然是正确的。我认为这是正确的,所以答案是:你不能。
如果您对详细信息感兴趣:
您的 Java 代码被转换为 Java 字节码,并且这些代码由 JVM 执行,JVM 是操作系统上运行的常规进程。而且您根本无法阻止操作系统中断线程(我假设您使用具有抢占式多任务处理功能的操作系统(每个 UNIX(包括 Linux 和 Mac OS X)、Windows 95 或更高版本,...))。
即使您可以在一个 Java 字节码中完成所有这些操作,它仍然无法按您希望的方式工作,因为操作系统可能会在一个 Java 字节码中间中断您。
即使是
awaitTermination(Date Deadline)
方法在这里也无济于事。它也必须由某人来实施。你能做的最好的事情就是用尽可能少的字节码来完成它。
如果我是你,我可能会像你的代码那样做。
然而,这个可能更精确一些:
但正如我所说:没有真正保证
shotdownNow()
在截止日期
立即执行。实际上,将deadline
设置为实际截止日期前一秒应该没问题:-)You probably don't mean 'atomically', I think you mean 'without delay' here. So that
timeDiff
is still correct when callingexec.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:
But as I said: There is no real guarantee
shotdownNow()
gets executed IMMEDIATELY atdeadline
. In reality, settingdeadline
to one second before the real deadline should be okay :-)在另一个高优先级线程(或计时器)中等待截止时间并调用 exec.shutdownNow()
Wait deadline in another high priority thread (or timer) and call
exec.shutdownNow()