title: How to have different synchronized blocks for different members of an Object in Java
body: <p>I am trying to understand synchronized block in java.</p>
  
  <p>In C/C++, we have mutex to protect a critical section. So, if an object has two resources say i and j, and if there are two interfaces SetI and SetJ which modify i and j respectively, i would declare two mutexes, each of which protect i and j, simple!</p>
  
  <p>Now i am trying to simulate the same thing in java.</p>
  
  <p>Consider a thread which enters synchronized(obj) and wants to modify its member i.</p>
  
  <p>Then can i not have another thread enter a different synchronized block but of the same object, which needs to modify other member of obj called j?</p>
  
  <p>I have attached a sample code which is trying to do the above task, but only one thread gets the chance and the other thread which wants to modify other variable never gets the chance.</p>
  
  <p>If the resources which are to be protected were objects i would have taken the synchronized on the members themselves, i and j. But since these are primitive data types, i can't use them in synchronized(i tried, but got compilation error). So, i am forced to synchronize on "this".</p>
  
  <p>The question is not why only one thread is getting the chance to execute which i understand. Rather, How do i achieve my requirement which is "two threads modifying two different resources of an object shouldn't block on each other".</p>
  
  <pre><code>class object
  {
      private boolean i;
      private boolean j;
  
      public void flipi(String arg)
      {
          synchronized(this)
          {
              while(true)
              {
                  i = !i;
                  System.out.println("flipi "+ "thread: " + arg + " value of i: " + i);
                  try
                  {
                      Thread.sleep(2000);
                  }
                  catch(Exception e)
                  {
                      System.out.println(e);
                  }
              }
          }
      }
  
      public void flipj(String arg)
      {
          synchronized(this)
          {
              while(true)
              {
                  j = !j;
                  System.out.println("flipj "+ "thread: " + arg + " value of j: " + j );
                  try
                  {
                      Thread.sleep(2000);
                  }
                  catch(Exception e)
                  {
                      System.out.println(e);
                  }
              }
          }
      }
  }
  
  class test extends Thread
  {
      object obj;
  
      test(object obj)
      {
          this.obj = obj;
      }
  
      @Override
      public void run()
      {
          String threadname = Thread.currentThread().getName();
  
          if(threadname.equals("test1"))
          {
              obj.flipi(Thread.currentThread().getName());
          }
          else if(threadname.equals("test2"))
          {
              obj.flipj(Thread.currentThread().getName());
          }
      }
  }
  
  public class testsynchronized
  {
      public static void main(String[] args)
      {
          object obj = new object();
  
          test t1 = new test(obj);
          test t2 = new test(obj);
  
          t1.setName("test1");
          t2.setName("test2");
  
          t1.start();
          t2.start();
      }
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>$ java testsynchronized
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  flipj thread: test2 value of j: false
  flipj thread: test2 value of j: true
  </code></pre>