[英]Unable to schedule a TimerTask a long time in advance
Heavily edited/updated: I am trying to schedule a task a long time in advance.大量编辑/更新:我试图提前很长时间安排任务。 I believe it is doing something to the process because it thinks it is not going to do anything else.
我相信它正在对流程做一些事情,因为它认为它不会做任何其他事情。 I should have used HOUR_OF_DAY for 24 hour time instead of hour, but still doesn't work correctly.
我应该将 HOUR_OF_DAY 用于 24 小时而不是小时,但仍然无法正常工作。 New code:
新代码:
public class OtherMainClass {
private static Timer timer;
private static Calendar cal;
public static void main(String[] args) throws InterruptedException {
cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 0);
System.out.println("Changed to: " + cal.getTime());
timer = new Timer();
System.out.println("current time is: " + Calendar.getInstance().getTime());
System.out.println("Scheduled for: " + cal.getTime());
timer.schedule(new MyTimerTask(), cal.getTime());
timer.schedule(new MyTimerCheckTask(), 0, 1000*60*10);
}
public static class MyTimerTask extends TimerTask {
@Override
public void run() {
System.out.println("in run at: " + Calendar.getInstance().getTime());
}
}
public static class MyTimerCheckTask extends TimerTask {
@Override
public void run() {
System.out.println("cal at: " + Calendar.getInstance().getTime() + " is: " + OtherMainClass.cal.getTime());
}
}
}
Commandline output (following "is:" is the calendar object passed into the schedule.):命令行输出(在“is:”之后是传递到日程表中的日历对象。):
Changed to: Fri Feb 14 08:00:41 GMT 2020
current time is: Thu Feb 13 23:25:41 GMT 2020
Scheduled for: Fri Feb 14 08:00:41 GMT 2020
cal at: Thu Feb 13 23:25:41 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
cal at: Thu Feb 13 23:35:41 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
cal at: Thu Feb 13 23:45:41 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
cal at: Thu Feb 13 23:55:41 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
cal at: Fri Feb 14 09:39:27 GMT 2020 is: Fri Feb 14 08:00:41 GMT 2020
in run at: Fri Feb 14 09:39:27 GMT 2020
Note the very interesting run at the end, as well as the other task running.请注意最后非常有趣的运行,以及正在运行的其他任务。 I clicked on commandline, which turned it into select mode, then control-c to exit that, then a few seconds later, it did both Tasks.
我单击命令行,将其转换为选择模式,然后使用 control-c 退出该模式,几秒钟后,它完成了两个任务。 (It should have run long before I clicked on commandline. Java version from
java -version
(在我单击命令行之前它应该已经运行很久了。来自
java -version
Java 版本
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.242-b08, mixed mode)
OS: Windows 10操作系统:Windows 10
I ran a test stating Friday around 11:30.我在星期五 11:30 左右进行了一项测试。 I changed the windows sleep settings set to 1 hour (Start -> Settings -> System -> Power & sleep).
我将 Windows 睡眠设置更改为 1 小时(开始 -> 设置 -> 系统 -> 电源和睡眠)。 Since Friday 8:00 had passed, I changed the day to Saturday.
自从星期五 8:00 过去了,我把日子改成了星期六。 I also changed the repeated interval to every half hour
3*1000*60*10
, since I wanted to limit the lines of output.我还将重复间隔更改为每半小时
3*1000*60*10
,因为我想限制输出行。 I ran the code in 5 different Command Prompt
s and started them at around 11:30 Friday:我在 5 个不同的
Command Prompt
运行代码,并在周五 11:30 左右启动它们:
Crtl-c
to stop it.Crtl-c
来停止它。 No printout to the screen after sleep initiated .Ctrl^c
.Ctrl^c
取消。 The prompt top bar changed to "Select Command Prompt.." and went back to "Command Prompt..".Ctrl^c
and the printouts were as if I had just started the program and it just printed the below.Ctrl^c
并且打印输出就好像我刚刚启动程序一样,它只是打印了下面的内容。 However I did not just exit the program, and it resumes the 30 min interval printout, as you can see from the below: This is the result of 4 and 5, that only started printing after i exited "Select Command Prompt"-mode at 9:43 on Saturday:这是 4 和 5 的结果,仅在我周六 9:43 退出“选择命令提示符”模式后才开始打印:
Changed to: Sat Feb 15 08:00:16 CET 2020
current time is: Sat Feb 15 09:49:13 CET 2020
Scheduled for: Sat Feb 15 08:00:16 CET 2020
in run at: Sat Feb 15 09:49:13 CET 2020
cal at: Sat Feb 15 09:49:13 CET 2020 is: Sat Feb 15 08:00:16 CET 2020
cal at: Sat Feb 15 10:19:13 CET 2020 is: Sat Feb 15 08:00:16 CET 2020
cal at: Sat Feb 15 10:49:13 CET 2020 is: Sat Feb 15 08:00:16 CET 2020
cal at: Sat Feb 15 11:19:13 CET 2020 is: Sat Feb 15 08:00:16 CET 2020
...
I ran another test with the windows sleep setting set to "Never".我在将 Windows 睡眠设置设置为“从不”的情况下进行了另一项测试。 Since Saturday 8:00 had passed, I changed the day to Sunday.
自从星期六 8:00 过去了,我把日子改成了星期天。 I again ran the code in 5 different
Command Prompt
s and started them at around 15:30 Saturday:我再次在 5 个不同的
Command Prompt
运行代码,并在星期六 15:30 左右启动它们:
Ctrl^c
.Ctrl^c
取消。 The prompt top bar changed to "Select Command Prompt.." and went back to "Command Prompt..".Ctrl^c
and the printout was identical to the test above.Ctrl^c
并且打印输出与上面的测试相同。 Result:结果:
Changed to: Sun Feb 16 08:00:31 CET 2020
current time is: Sat Feb 15 15:35:31 CET 2020
Scheduled for: Sun Feb 16 08:00:31 CET 2020
cal at: Sat Feb 15 15:35:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
cal at: Sat Feb 15 16:05:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
..snip..
cal at: Sun Feb 16 07:05:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
cal at: Sun Feb 16 07:35:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
in run at: Sun Feb 16 08:00:31 CET 2020
cal at: Sun Feb 16 08:05:31 CET 2020 is: Sun Feb 16 08:00:31 CET 2020
The answer by Shaun Rowan to Why is my command prompt freezing on Windows 10? Shaun Rowan对为什么我的命令提示符在 Windows 10 上冻结的答案? you provided mentions:
你提供了提及:
whenever you click on a command window in windows 10, it immediately halts the application process when it attempts to write to the console
每当您单击 Windows 10 中的命令窗口时,它会在尝试写入控制台时立即停止应用程序进程
So it doesn't seem to be a bug in the Command Prompt
either, rather it's a feature
.所以它似乎也不是
Command Prompt
的错误,而是一个feature
。
As per Will my Window 10 apps continue to update or download on sleep or hibernation mode?根据我的 Window 10 应用程序是否会在睡眠或休眠模式下继续更新或下载? by A. User :
由A. 用户:
Sleep is a power-saving state that allows a computer to quickly resume full-power operation (typically within several seconds) when you want to start working again.
睡眠是一种省电状态,当您想再次开始工作时,它允许计算机快速恢复全功率运行(通常在几秒钟内)。 Putting your computer into the sleep state is like pausing a DVD player—the computer immediately stops what it's doing and is ready to start again when you want to resume working.
将计算机置于睡眠状态就像暂停 DVD 播放器一样 — 计算机会立即停止正在执行的操作,并准备在您想要继续工作时重新启动。
If you're very interested in Windows sleep modes TechRepublic has an article on Investigating Sleep states in Windows 10 describes the 6 stages of sleep, from when the (processor clocks are stopped) -> (the processor shuts off to save power) -> (other chips on the motherboard may shut off)如果您对 Windows 睡眠模式非常感兴趣,TechRepublic 有一篇关于Investigating Sleep states in Windows 10的文章描述了睡眠的 6 个阶段,从(处理器时钟停止)->(处理器关闭以节省电量)-> (主板上的其他芯片可能会关闭)
Conclusion:结论:
If you want your java program to keep executing, ensure your Command Prompt window is not in "Select Command Prompt.."-mode and also ensure that your computer does not enter sleep mode, as either of these will halt execution of the program.如果您希望您的 java 程序继续执行,请确保您的命令提示符窗口不在“选择命令提示符..”模式下,并确保您的计算机不会进入睡眠模式,因为这两种模式中的任何一个都会停止程序的执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.