I want to print out "Hello World"
only when both the below conditions are true;
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:
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.