title: Thread calling non-synchronized instance method when a synchronized method is called
body: <pre><code>public class Common {      
      public synchronized void synchronizedMethod1() {
          System.out.println("synchronizedMethod1 called");
          try {
              Thread.sleep(1000);
              } catch (InterruptedException e) {
              e.printStackTrace();
          }
          System.out.println("synchronizedMethod1 done");
      }
      public void method1() {
          System.out.println("Method 1 called");
          try {
              Thread.sleep(1000);
              } catch (InterruptedException e) {
              e.printStackTrace();
          }
          System.out.println("Method 1 done");
      }
  }
  
  
  
  public class MyThread extends Thread {
      private int id = 0;
      private Common common;
  
      public MyThread(String name, int no, Common object) {
          super(name);
          common = object;
          id = no;
      }
  
      public void run() {
          System.out.println("Running Thread" + this.getName());
          try {
              if (id == 0) {
                  common.synchronizedMethod1();
                  } else {
                  common.method1();
              }
              } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          Common c = new Common();
          MyThread t1 = new MyThread("MyThread-1", 0, c);
          MyThread t2 = new MyThread("MyThread-2", 1, c);
          t1.start();
          t2.start();
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Running ThreadMyThread-1  
  synchronizedMethod1 called  
  Running ThreadMyThread-2  
  Method 1 called  
  synchronizedMethod1 done  
  Method 1 done  
  </code></pre>
  
  <p>I would like to find a way to prevent method1() from running when i called synchronizedMethod1. Unless I'm mistaken all methods are called and Java compiles them during and before runtime regardless if it's synchronized or not. </p>
  
  <p>Should i have used a Lock object instead and/or not also make method1() a synchronized method?</p>