title: Does Java monitor include instance variables?
body: <p>Is it that monitor in Java does not restrict access to instance variables and only to the methods which are declared <em>synchronized</em> or code in <em>synchronized</em> statements?</p>
  
  <p>I have created two threads, <code>thread y</code> invokes <code>sync()</code> method, which is declared <em>synchronized</em> while <code>thread r</code> invokes <code>unsync()</code> method which is not declared synchronized. Both invoke methods on shared object <code>s</code>. </p>
  
  <p><code>Thread r</code> is able to modify the instance variable of object <code>s</code> while the <em>monitor</em> or <em>lock</em> of that object is still being held by the <code>thread y</code>. </p>
  
  <p>Is it that the monitor in Java does not restrict access to instance variables, and only to the methods which are declared <em>synchronized</em> or code in <em>synchronized</em> statements?</p>
  
  <pre><code>public class Stuff {
  
      private int a = 10;
  
      public synchronized void sync() {
          long t1 = System.currentTimeMillis();
          System.out.println("Okay, I am in sync() method. "
                          + "I will be waiting for 10 seconds. Current Time = "
                          + System.currentTimeMillis());
          while (System.currentTimeMillis() - t1 &lt; 10000);
          System.out.println("Okay, I have waited for 10 seconds. Current time is "
                          + System.currentTimeMillis()
                          + ". Now I will exit from sync() method, a = " + this.a);
      }
  
      public void unsync() {
          System.out.println("Alright, I am in unsync() method. The current time is "
                          + System.currentTimeMillis());
          this.a = this.a + 1;
          System.out.println(". The time of exit from unsync() method is "
                          + System.currentTimeMillis());
  
      }
  }
  
  class T1 extends Thread {
  
      Stuff s;
  
      public T1(Stuff s) {
          this.s = s;
      }
  
      public void run() {
          s.sync();
      }
  }
  
  class T2 extends Thread {
  
      Stuff s;
  
      public T2(Stuff s) {
          this.s = s;
      }
  
      public void run() {
          s.unsync();
      }
  }
  
  class Main {
  
      public static void main(String args[]) throws Exception {
          Stuff s = new Stuff();
          T1 y = new T1(s);
          T2 r = new T2(s);
          y.start();
          Thread.sleep(2000);
          r.start();
      }
  }
  </code></pre>
  
  <p>The output of the program is below:</p>
  
  <blockquote>
  <pre> 
  Okay, I am in sync() method. I will be waiting for 10 seconds. Current Time = 1358801766310  
  Alright, I am in unsync() method. The current time is 1358801768343. The time of exit from unsync() method is 1358801768343  
  Okay, I have waited for 10 seconds. Current time is 1358801776310. Now I will exit from sync() method, a = 11
  </pre>
  </blockquote>