title: Method Arguments and Local Variable are Thread Safe
body: <p>Can anyone confirm below statement?</p>
  
  <blockquote>
    <p>Method Arguments and Local Variable declared inside a method is Thread
    safe; both are exclusive for each invocation of the Thread.</p>
    
    <p>On the other hand Global Variable is shared.</p>
  </blockquote>
  
  <p>For instance</p>
  
  <pre><code>class MyThread implements Runnable{
  
      int _tmp = 10;      //SHARED BETWEEN t1, t2, t3, and so on
  
      public void run(){
  
          testThreadConcurrency(arg); // arg  is EXCLUSIVE FOR ALL invocation of testThreadConcurrency
      }
  
      public void testThreadConcurrency (int num){
  
          int tmp = 10;   //EXCLUSIVE FOR ALL invocation of testThreadConcurrency()
  
      }
  }
  
  
  public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          MyThread _runn = new MyThread();
  
          Thread t1 = new Thread(_runn,"t1");
          Thread t2 = new Thread(_runn,"t2");
          Thread t3 = new Thread(_runn,"t3");
          t1.start();
          t2.start();             
          t3.start()
  }
  </code></pre>
  
  <p>**</p>
  
  <blockquote>
    <p>Please check the output of below program which prove that Global
    variables are SHARED</p>
  </blockquote>
  
  <p>**</p>
  
  <pre><code>public class ThreadDemo {
  
      static Object _lock = new Object();
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          MyThread _runn = new MyThread();
  
          Thread t1 = new Thread(_runn,"t1");
          Thread t2 = new Thread(_runn,"t2");
          Thread t3 = new Thread(_runn,"t3");
          t1.start();
          try {Thread.sleep(300);} catch (InterruptedException e1) {}
          t2.start(); 
          try {Thread.sleep(300);} catch (InterruptedException e1) {}
          t3.start(); 
  
          try {Thread.sleep(300);} catch (InterruptedException e1) {}
          synchronized(ThreadDemo._lock){ThreadDemo._lock.notify();}
  
          try {Thread.sleep(300);} catch (InterruptedException e1) {}
          synchronized(ThreadDemo._lock){ThreadDemo._lock.notify();}
  
  
          try {Thread.sleep(300);} catch (InterruptedException e1) {}
          synchronized(ThreadDemo._lock){ThreadDemo._lock.notify();}
  
  
      }
  
  }
  
  class MyThread implements Runnable{
  
      int _tmp = 10;
  
      public void run(){
  
          testTestConcurrency();
      }
  
      public void testTestConcurrency(){
  
          if(Thread.currentThread().getName().equals("t2"))
          {
              _tmp = 20;          
          }
  
          synchronized(ThreadDemo._lock){try {ThreadDemo._lock.wait();} catch (InterruptedException e) {}}
  
          System.out.println("_tmp = "+_tmp+"|"+Thread.currentThread().getName());
  
  
      }
  }
  </code></pre>