title: Regarding the synchronized function
body: <p>I was interviewed and I was asked about a class with two functions m1() and m2(). m1() is synchronized and m2() is not and two threads t1 and t2 are in trying to access this object. Now if t1 is accessing the m1() function, then can t2 access the m2() method at the same time?</p>
  
  <p>I said <em>yes</em> it should be allowed to access it as there is no lock on m2() method, but the interviewer insisted that as the lock is on the same object it will not allowed to access the m2() function to t2 thread.</p>
  
  <p>For this I wrote a program to check and found that whatever I was saying was right. Please see the program with the output.</p>
  
  <pre><code>class myExperiment2 implements Runnable{
      myThreadEx m;
  
      myExperiment2(myThreadEx m){
          this.m = m;
      }
  
      @Override
      public void run() {
          try {
              m.m2();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  }
  
  class myExperiment1 implements Runnable{
      myThreadEx m;
  
      myExperiment1(myThreadEx m){
          this.m = m;
      }
  
      @Override
      public void run() {
          try {
              m.m1();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  }
  
  class myThreadEx{
  
      synchronized void m1() throws InterruptedException {
          System.out.println("1 am in m1....1");
          Thread.sleep(10000);
          System.out.println("I am in m1()");
          Thread.sleep(10000);
          System.out.println("1 am in m1....2");
      }
  
      void m2() throws InterruptedException{
          System.out.println("1 am in m2....1");
          Thread.sleep(5000);
          System.out.println("i am in m2()");
          Thread.sleep(5000);
          System.out.println("1 am in m2....2");
      }
  }
  
  public class ThreadEx {
  
      public static void main(String args[]){
          myThreadEx m = new myThreadEx();
  
          Thread t1 = new Thread(new myExperiment1(m));
          Thread t2 = new Thread(new myExperiment2(m));
  
          t1.start();
          t2.start(); 
      }
  }
  </code></pre>
  
  <p>The output is </p>
  
  <blockquote>
    <p>1 am in m1....1<br>
    1 am in m2....1<br>
    i am in m2()<br>
    1 am in m2....2<br>
    I am in m1()<br>
    1 am in m1....2  </p>
  </blockquote>
  
  <p>Can some one suggest me if I am missing something or if there is possible scenario where I can be wrong and whatever interview was saying, was right?</p>