title: How to determine if thread is doing an action
body: <p>I have simple class thats extends thread class it has run method which looks like this:</p>
  
  <pre><code>public void run() {
  
      while(running) {
  
          if(enabled)
          {
              doStep();
              enabled = false;
          }
          else
          {
              try {
                  this.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>In other place in my code I'm calling, when I want <code>myObject</code> to call <code>doStep()</code></p>
  
  <pre><code>myObject.enbled = true;
  </code></pre>
  
  <p>But how can make i sure that <code>myObject</code> actually has finished <code>doStep()</code> method? simple</p>
  
  <pre><code>myObject.enable = true
  while(myObject.isEnabled())
  {
  ;
  }
  doActionAfterObjectFinish();
  </code></pre>
  
  <p>sometime cause program to stuck in the loop, and for sure <code>doStep()</code> does not cause it.</p>
  
  <p>@thanks for answers instead of using sempahores woudlnt it help if i change code in such way ?</p>
  
  <pre><code>    public void run() {
  
          while(running) {
  
              if(enabled)
              {
                  synchronized(this){
                  doStep();
                  enabled = false;
  }
              }
              else
              {
                  try {
                      this.sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
  
  public synchronized boolean isEnabled() {
  return enabled;
  }
  </code></pre>