title: Thread Synchronization
body: <p>I have 2 threads in my program as follows:</p>
  
  <pre><code>class SendThread implements Runnable {
       public void run(){
             Thread.sleep(1000);
  
             while (true){    
                  if (CONNECTION_ESTABLISHED){
                      // code here
                  }
             } 
       } 
   } 
  
  class ReceiveThread implements Runnable {
       public void run(){
  
  
             while (true){    
                  // code here
                  CONNECTION_ESTABLISHED = true;  
  
             }
       }
   }
  </code></pre>
  
  <p>I have defined <code>CONNECTION_ESTABLISHED</code> as a <code>static Boolean</code>.</p>
  
  <p>In the second thread, the <code>Boolean</code> <code>CONNECTION_ESTABLISHED</code> is set to <code>true</code> at a certain point. If I don't use <code>Thread.sleep(1000)</code> in the 1st thread, after <code>CONNECTION_ESTABLISHED</code> is set to <code>true</code> in the 2nd thread I don't enter the relevant <code>if</code> statement in the 1st thread.</p>
  
  <p>Is there another way around this? Because my 1st thread will often be dependent on variable changes in the second thread.</p>