title: Is it okay to call notify() on a lock object before calling wait() on it?
body: <p>I am using wait() and notify() in my code and there is a scenario where the execution can reach notify() first before a wait is actually called on the lock object on another thread.  I created a flag "isWaitCalled" in the lock object and using it to skip notify() call if wait() is not called already. Is this flag "isWaitCalled" redundant?  Is it okay if notify() is called before wait in one of the scenarios?
  <strong>Thread A:</strong></p>
  
  <pre><code>synchronized (syncObject) {
            try {
                  if (!syncObject.isLoadComplete) {
                      syncObject.isWaitCalled = true;
                      syncObject.wait();
                  }
            } catch (Exception ex) {
            }                                              
      }
  </code></pre>
  
  <p><strong>ThreadB:</strong></p>
  
  <pre><code>synchronized (syncObject) {
            try {
                  if (!syncObject.isLoadComplete) {
                    syncObject.isLoadComplete =true;
                     if (syncObject.isWaitCalled) {
                         syncObject.notify();         
                     }                  
                 }
            } catch (Exception ex) {
            }                                              
      }
  </code></pre>