title: How to lock a variable used in multiple threads
body: <p>I have asked a question badly over here <a href="https://stackoverflow.com/questions/4081986/lock-on-a-variable-in-multiple-threads">Lock on a variable in multiple threads</a> so for clarity I am going to ask it here and hope I can ask it correctly.</p>
  
  <pre><code>classA
    creates instance of classB
    has methodA that can update &amp; uses classB's myVar
    has methodB that can update &amp; uses classB's myVar
  
  classB
    has a variable myVar
  </code></pre>
  
  <p>methodA and methodB can both run in separate threads (called in new threads from main). How do I ensure this is thread safe?</p>