I am trying to do a simple timeout in case of unsuccessful login. Here is the pseudocode.
class A {
public A() {
int timeOutSeconds = 10;
try {
B b = new B(timeOutSeconds);
}catch (Exception e){
//detect Time out as well
}
}
}
class B{
boolean loggedIn = false;
public B(int seconds) {
asyncLogin(); //this will set loggedIn true
//how do I use seconds here
long startTime = System.currentTimeMillis();
while (!isLoggedIn()) {
if(System.currentTimeMillis() - startTime > (seconds*1000)){
throw new RuntimeException("Time Out");
}
}
}
private boolean isLoggedIn() {
return loggedIn; //set asynchronously
}
}
Is there a standard way of doing this?
I have gone through the answers here and here but they don't look as simple or did same as what I did.
I would be interested in knowing why my method is not that good if there are better/standard ways to do the same.