title: Can we use synchronised for run() method in java?
body: <p>As we know there is no provision to prevent more than one thread calling the <code>run()</code> method by using <code>start()</code> method. I did create two objects <code>m1</code> and <code>m2</code> both calling the same thread to run. </p>
  
  <p>I need to make sure the first object finishes (<code>m1.start</code>) its execution by calling thread before second object execution starts. </p>
  
  <p>My question is why can't i use <code>synchronized</code> keyword with <code>run()</code> method in the thread that I have created (i.e. <code>MyThread1</code>)?</p>
  
  <p>I tried using "synchronized" to run() method in the thread that I have created but it gives arbitrary output (in other words <code>m2</code> doesn't wait for the <code>m1</code> to finish execution).</p>
  
  <p>You can see at the very bottom of the program the output that I am getting. </p>
  
  <pre><code>public class ExtendedThreadDemo {
  
      public static void main(String[] args) {    
          Mythread1 m1 =new Mythread1();
          Mythread1 m2 =new Mythread1();
          m1.start();
          m2.start();
          System.out.println(" main thread exiting ....");
      }
  }
  </code></pre>
  
  <h3>MyThread</h3>
  
  <pre><code>public class MyThread1 extends Thread {
  
      public synchronized void run() {
          for(int i=1; i&lt;5; i++) {
              System.out.println(" inside the mythread-1 i = "+ i);
              System.out.println(" finish ");
  
              if (i%2 == 0) {
                  try {
                      Thread.sleep(1000);
                  } catch(InterruptedException e) {
                       System.out.println(" the thread has been interrupted ");
                  }
              }
          }
      }
  }
  </code></pre>
  
  <h3>Output</h3>
  
  <pre><code>main thread exiting ....
  inside the mythread-1 i = 1
  finish
  inside the mythread-1 i = 2
  finish 
  inside the mythread-1 i = 1
  finish 
  inside the mythread-1 i = 2
  finish 
  inside the mythread-1 i = 3
  finish 
  inside the mythread-1 i = 4
  finish 
  inside the mythread-1 i = 3
  finish 
  inside the mythread-1 i = 4
  finish 
  </code></pre>
  
  <p>As you can see after <code>i = 2</code>, second object (i.e. <code>m2.start()</code>) started executing.</p>