title: Setting "flags" between threads in Java
body: <p>I have a class which connects to a server as below.</p>
  
  <pre><code>@Override
  public void run() {
      while (running) {
          try {
              msgHandler.log("Connecting to " + host + ":" + port);
              Socket s = new Socket(host, port);
  
              if (s.isConnected() &amp;&amp; !s.isClosed()) {
                  msgHandler.connectionInit(s);
              }
              BufferedInputStream input = new BufferedInputStream(s.getInputStream());
  
      }
  }
  </code></pre>
  
  <p>The consumer which is the msgHandler, frequently polls the socket if a connection ever goes down as below.</p>
  
  <pre><code>@Override
  public void connectionInit(Socket s) throws IOException {
      logger.info("Connected to AWW Service on " + configuration.getAwwHost() + ":" + configuration.getAwwPort());
  
      output = new BufferedOutputStream(s.getOutputStream());
  
      connector.componentReady();
      Timer t = new Timer();
      t.schedule(new TimerTask() {
          @Override
          public void run() {
                  try {
                      pollServer();
                  } catch (IOException e) {
                      // SOCKET GETS BROKEN HERE 
                  }
          }
      }, 0, 25000);
  
  }
  </code></pre>
  
  <p>Question is, how can i communicate from the exception i get when the socket connection gets broken back to the run() thread, so it can try to reinitialize the socket and the input stream?</p>
  
  <p>I dont think a notify() or wait() mechanism is appropriate here as wait() will just put the run() thread to sleep. 
  I was thinking whats the equivalent of setting a flag when the connection gets broken, and the run() thread constantly checks the flag, and when it is set to true, it reinitialize the socket. But i am sure there would be a more efficient multi threading approach native to java for achieving this.</p>