How to make this algorithm better and working, where I am always ending up to reallyFail();. And I cannot let it in while loop for an unlimited amount of time, only for 5 minutes at most.
// This is a heavy rendering encryption which takes sometimes 1 minute
// or sometimes
// it takes less then 1 seconds, very random to estimate to a fix time or delay
prepareEncoding(letter);
// Now once that is done i have two values which if null or empty
// Goal can not be accomplished
if (main.myencryption0 != null && main.myencryption1 != null) {
// Once those value are available
// Proceed to he Goal
prepareToGoal(letter);
} else {
// Value is empty or null we do not know the status
// We try for 3 times the same thing
// (i think this is wrong, but could not find alternative best way)
for (int i = 0; main.myencryption0 == null && main.myencryption1 == null && i <= 3; ++i) {
prepareEncoding(letter);
}
// Wait still for few seconds to make 100% sure
// We can not wait more then 5 minute, because its too long.
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
// Finally again check same thing do or die
if (main.myencryption0 != null && main.myencryption1 != null) {
prepareToGoal(letter);
} else {
reallyFail(); // OK - give up, drop the ball
}
}
You can check the time with System.currentTimeMillis. So you can leave the while after 5 min.