Unable to schedule a TimerTask a long time in advance

tyhdefu :

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. 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.):

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. (It should have run long before I clicked on commandline. Java version from java -version

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

Scratte :

I ran a test stating Friday around 11:30. I changed the windows sleep settings set to 1 hour (Start -> Settings -> System -> Power & sleep). Since Friday 8:00 had passed, I changed the day to Saturday. I also changed the repeated interval to every half hour 3*1000*60*10, since I wanted to limit the lines of output. I ran the code in 5 different Command Prompts and started them at around 11:30 Friday:

  1. No QuickEdit Mode. The program was running and I had to Crtl-c to stop it. No printout to the screen after sleep initiated. (Admittedly I didn't wait to see if it would print the 8 o'clock message after some time, because it seemed pointless.)
  2. QuickEdit Mode. Did not click inside the screen. Same as 1.
  3. QuickEdit Mode. Clicked inside the screen and immediately cancelled with Ctrl^c. The prompt top bar changed to "Select Command Prompt.." and went back to "Command Prompt..". Same as 1.
  4. QuickEdit Mode. Clicked inside the screen and highlighted some test. The prompt top bar changed to "Select Command Prompt". There was absolutely no printout after that until I pressed Ctrl^c and the printouts were as if I had just started the program and it just printed the below. However I did not just exit the program, and it resumes the 30 min interval printout, as you can see from the below:
  5. QuickEdit Mode. Clicked inside the screen (that's all). The prompt top bar changed to "Select Command Prompt". Same as 4.

This is the result of 4 and 5, that only started printing after i exited "Select Command Prompt"-mode at 9:43 on Saturday:

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". Since Saturday 8:00 had passed, I changed the day to Sunday. I again ran the code in 5 different Command Prompts and started them at around 15:30 Saturday:

  1. No QuickEdit Mode. Worked without issues. See below.
  2. QuickEdit Mode. Did not click inside the screen. Same as 1.
  3. QuickEdit Mode. Clicked inside the screen and immediately cancelled with Ctrl^c. The prompt top bar changed to "Select Command Prompt.." and went back to "Command Prompt..". Same as 1.
  4. QuickEdit Mode. Clicked inside the screen and highlighted some test. The prompt top bar changed to "Select Command Prompt". There was absolutely no printout after that. Until I pressed Ctrl^c and the printout was identical to the test above.
  5. QuickEdit Mode. Clicked inside the screen (that's all). The prompt top bar changed to "Select Command Prompt". There was absolutely no printout after that.

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? 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

So it doesn't seem to be a bug in the Command Prompt either, rather it's a feature.

As per Will my Window 10 apps continue to update or download on sleep or hibernation mode? by A. User:

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.

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)


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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=421816&siteId=1