1

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.

impossible
  • 2,380
  • 8
  • 36
  • 60
  • Possible duplicate of [How to timeout a thread](https://stackoverflow.com/questions/2275443/how-to-timeout-a-thread) – XtremeBaumer Apr 05 '18 at 06:39
  • 1
    you may want to sleep (or add some delay) in your very tight while loop – Scary Wombat Apr 05 '18 at 06:40
  • unsuccesfull login in what web application, normal java or what is your exact requirement – Rishal Apr 05 '18 at 06:40
  • In case it's web app you can always configure to idle timeout in your server.xml file 30 – Yati Sawhney Apr 05 '18 at 06:42
  • your process and code is not clear enough, what do you want to do? a timeout for? – Jonnathan Q Apr 05 '18 at 06:42
  • How does unsuccessful login and timeout relate to each other? If you're talking about timing out a network connection, that can be done in a correct way with a connect or read timeout. There's no general "time out things" functionality, although you can sometimes get close by interrupting a thread. – Kayaman Apr 05 '18 at 06:47
  • Is there an observer pattern we are looking at? – Prashant Apr 05 '18 at 06:49
  • As I have commented, loggedIn is boolean and it gets set asynchronously. I am waiting it to get set. And if it doesn't get set in 10sec I want to do a timeout. (updating the question) – impossible Apr 05 '18 at 06:53
  • There are not many things worse than a rogue thread reaching into a class and directly updating a variable. Start with a basic Java book; syntax != programming. – Abhijit Sarkar Apr 05 '18 at 07:00
  • Why is the login asynchronous and what does it do? You're certainly not getting anything out of it being asynchronous, since you're blocking and waiting for the results. – Kayaman Apr 05 '18 at 07:02
  • @Kayaman : Login is async as it is done by creating session, handshake, few local properties setting etc. It is a network call and can take time. Class B is a wrapper that has login as a task in it. Class B completes it creation only after login is successful, otherwise it should not be instantiated, hence I am blocking and waiting to get the login thing done. – impossible Apr 05 '18 at 08:44
  • @AbhijitSarkar: Variable loggedIn is updated by a callback from asyncLogin(). It is not a rogue thread. – impossible Apr 05 '18 at 08:45
  • If you don't show real code, you won't get real help. Don't post hello world type stuff. [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Abhijit Sarkar Apr 05 '18 at 09:01

1 Answers1

0

A somewhat proper way would be to forget all that busy waiting and other horrible code and just notify the thread when/if the login task finishes. Something along the lines of

private final Object lock = new Object();
boolean loggedIn = false;
public B(int seconds) {
    asyncLogin();

    synchronized(lock) {
        lock.wait(seconds * 1000);
    }
    if(!loggedIn) // Timeout expired without login
        throw new TimeoutException("Couldn't login");
    ... // Do things
}

public void logIn() {  // Called by the login thread
    synchronized(lock) {
        loggedIn = true;
        lock.notifyAll();
    }
}
Kayaman
  • 72,141
  • 5
  • 83
  • 121