简体   繁体   中英

print hello world only when conditions are met

I want to print out "Hello World" only when both the below conditions are true;

  • If current hour is greater than or equal to 8PM.
  • And if isDone becomes true. This is coming from another service, basically this is to check whether some process finished or not.

Otherwise I will keep sleeping until both the above conditions become true. Below is the code I have:

public static void main(String[] args) {
    DateTime dt = new DateTime();
    int hour = dt.getHourOfDay();
    if (hour < 20) {
        TimeUnit.HOURS.sleep(20 - hour);
    }

    boolean isDone = checkStatus();
    while (!isDone) {
        TimeUnit.MINUTES.sleep(15);
        isDone = checkStatus();
    }

    // now print out
    System.out.println("Hello World");
}

As you can see, I have two different conditions separately which does the check and then at the end I print out. Is there any better way to write above code to accomplish same thing?

It doesn't have to be exactly accurate. I am calling checkStatus method every 15 minutes to see whether isDone became true or not.

There is a bug in the application, where if it happens to be after 8 AM, but the checkStatus doesn't return true before midnight, then it will continue executing during the day.

Put both conditions inside the loop together:

boolean isDone = false;
while (!isDone) {
    int hour = new DateTime().getHourOfDay();
    isDone = hour >= 20;
    if (!isDone) TimeUnit.HOURS.sleep(20 - hour);

    isDone = checkStatus();
    if (!isDone) TimeUnit.MINUTES.sleep(15);
}

System.out.println("Hello World");

May be this way helps you.

public boolean CheckHour(){

DateTime dt = new DateTime();
int hour = dt.getHourOfDay();
if (hour < 20) {        
        TimeUnit.HOURS.sleep(20 - hour);            
        return false;       
}
return true;

}


boolean isDone = checkStatus();
while (!isDone || !CheckHour()) {

TimeUnit.MINUTES.sleep(15);
isDone = checkStatus();
}

// now print out
System.out.println("Hello World");

The requirements for your code are not entirely clear since your original post does not specify whether you want your code to stop if checkStatus does not return true by midnight. I wrote the following solution based on your original post which states:

Otherwise I will keep sleeping until both the above conditions become true

I take the above quote to mean you want your program to sleep at all times unless it is 8PM or later (local time) and the checkStatus method returns true .

Before I post any code I'd like to point out 3 things:

  1. Your original code, if invoked late in the hour - say for example 4:59pm will sleep 20-(current hour) until 8PM or later. So it will sleep from 4:59 to 8:59 since 20 - 16 = 4. You lose almost an entire hour that you could have been checking the status of your external process. Having your code sleep for 1 minute at a time should not impact CPU usage that much - if you're worried about this then you really need to get a faster CPU or free up some CPU - and would prevent you from losing so much time if you invoke your program near the end of the hour.
  2. I don't know why you're using Joda time when you could very easily use Java APIs included with the JRE to accomplish what you want to do. In my code I have used Java APIs because I don't like using external APIs without good reason.
  3. Your code does not handle the situation where the program is interrupted during either of the sleep cycles. I have added try-catch blocks in my code below, but not sure what you prefer to do in this situation so I have left in the TODO auto generated comments.

Here is the code I have come up with (I added some System.out calls to help illustrate what is happening):

import java.time.LocalTime;
import java.util.concurrent.TimeUnit;
...

private static final LocalTime EIGHT_PM = LocalTime.of(20, 0);

public static void main(String[] args) {
    while(true){
        LocalTime now = LocalTime.now();
        if(now.isBefore(EIGHT_PM)){
            System.out.println(now + " It is before 8PM, sleeping 1 minute");
            try {
                TimeUnit.MINUTES.sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else if(checkStatus()){
            System.out.println("It is 8PM or later and external process is done.");
            break;
        }else{
            System.out.println("It is 8PM or later, but external process is not done. Sleeping 15 minutes.");
            try {
                TimeUnit.MINUTES.sleep(15);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    // now print out
    System.out.println("Hello World");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM