title: Simple function call timeout in java
body: <p>I am trying to do a simple timeout in case of unsuccessful login.
  Here is the pseudocode.</p>
  
  <pre><code>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 &gt; (seconds*1000)){
                  throw  new RuntimeException("Time Out");
              }
          }
      }
      private boolean isLoggedIn() {
          return loggedIn; //set asynchronously
      }
  }
  </code></pre>
  
  <p>Is there a standard way of doing this?</p>
  
  <p>I have gone through the answers <a href="https://stackoverflow.com/questions/19456313/simple-timeout-in-java">here</a> and <a href="https://stackoverflow.com/questions/8982420/time-out-method-in-java">here</a> but they don't look as simple or did same as what I did.</p>
  
  <p>I would be interested in knowing why my method is not that good if there are better/standard ways to do the same.</p>